Divide one array in two table view cell - arrays

I have an array, let's call it list
var list = ["name1", "name2", "name3", "name4", "name5"]
I have two prototype cells, the first one should have the first index of array in its title and the second should should has the rest
In both cells, I wrote indexPath.row, could anyone help me to how to divide this array to use in both cells.
In another word, I want to have 5 cell in my table view
cell 1 with Identifier: "list1" should have these items in its title = ["name1", "name2", "name3"]
cell 2 with Identifier: "list2" should have these items in its title = [ "name4", "name5"]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "list1", for: indexPath) as? ListCell {
cell.list1Title?.text = list[indexPath.row]
return cell
} else if let cell = tableView.dequeueReusableCell(withIdentifier: "list2", for: indexPath) as? ListCell {
cell.list2Title?.text = list[indexPath.row]
return cell
}
return TableViewCell()
}

Assuming you want 5 rows to appear in the table view, the following will show the first three values using cell "list1" and the remaining values using cell "list2":
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "list1", for: indexPath) as! ListCell
cell.list1Title?.text = list[indexPath.row]
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "list2", for: indexPath) as! ListCell
cell.list2Title?.text = list[indexPath.row]
return cell
}
}
Of course this assumes your ListCell has both list1Title and list2Title properties. Normally you would actually have two different cell classes for the two types which means you use two different casts instead of casting both to ListCell.

Related

How to sort my array in my tableview by Date

I am trying to return the following cells sorted by date. I have searched many posts but I can't really understand where I specifically put the sort by:
let sortedArray = jogs.sorted { $0.jogDate < $1.jogDate }
This is the function I use in my TableViewController:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCellIdentifier") as? ToDoCell else {
fatalError("Could not dequeue a cell")
}
cell.delegate = self
let jog = jogs[indexPath.row]
cell.titleLabel?.text = jog.title
cell.descriptionLabel.text = jog.notes
cell.dateLabel.text = Jog.jogDateFormatter.string(from: jog.jogDate)
return cell
}
Or should I put the sort in the Struct file? Also would a Class file be better than a Struct file?
1. Sort your jogs by date
override func viewDidLoad() {
super.viewDidLoad()\
jogs.sort { $0.jogDate < $1.jogDate }
tableView.reloadData()
}
or
2. Use your sorted array in UITableViewDelegate / Datasource implements
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCellIdentifier") as? ToDoCell else {
fatalError("Could not dequeue a cell")
}
cell.delegate = self
let jog = sortedArray[indexPath.row] // sorted array
cell.titleLabel?.text = jog.title
cell.descriptionLabel.text = jog.notes
cell.dateLabel.text = Jog.jogDateFormatter.string(from: jog.jogDate)
return cell
}

Use arrays from tableView if statement in another viewcontrollers

I need to create two "help" arrays according to if results in table view.
My main problem is: How can I use this arrays in another view controller?
etc.: Beacause when I want to use one array in another controller with TableViewController().helpArrayOne it is empty/or not exist in tableViewController, but after print in tableViewController I get values, thanks.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
let listOfStudents = allUrl.reduce(into: [String:String]()){$0[$1.id] = $1.stringValue}
for key in listOfStudents{
// Compare data
if cell.textLabel?.text == key.key {
cell.imageView!.image = UIImage(named:"false_icon")
var helpArrayOne:[String] = []
helpArrayOne.append(all[indexPath.row].id)
var helpArrayTwo:[String] = []
helpArrayTwo.append(all[indexPath.row].stringValue)
break
} else {
cell.imageView!.image = UIImage(named:"true_icon")
}
}
print(helpArrayOne,helpArrayTwo)
return cell
}
Remember scope; declare the arrays as instance variables that are optionals. Then you will be able to pass them into another view controller from anywhere else in the scope of the current view controller.
Just make sure that they are being passed after the value has been assigned to them.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { [weak self] in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
let listOfStudents = allUrl.reduce(into: [String:String]()){$0[$1.id] = $1.stringValue}
for key in listOfStudents{
// Compare data
if cell.textLabel?.text == key.key {
cell.imageView!.image = UIImage(named:"false_icon")
self?.helpArrayOne.append(all[indexPath.row].id)
self?.helpArrayTwo.append(all[indexPath.row].stringValue)
break
} else {
cell.imageView!.image = UIImage(named:"true_icon")
}
}
print(helpArrayOne,helpArrayTwo)
return cell
}

Display only similar compared data into table view

I have question which is little bit similar as my previous, but different.
There is a code description:
I have 2 Json arrays which I try to compare in my code -> all and allUrl. When array all contains some id from array allUrl image table row should be change to red, or vice versa green.
And my new question:How can I display only "red_icon" data in table?This data should be similar for this two arrays.thanks
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = all[indexPath.row].id
cell.detailTextLabel?.text = all[indexPath.row].timestampValue
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){$0[$1.id] = $1.timestampValue}
// Compare data
listOfStudentsUrl.forEach{ key in print(key)
if cell.textLabel?.text == key.key {
cell.imageView!.image = UIImage(named:"red_icon")
break
}else{
cell.imageView!.image = UIImage(named:"green_icon")
}}
return cell
}
Correct working code after discussion below:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){$0[$1.id] = $1.timestampValue}
// Compare data
listOfStudentsUrl.forEach{ key in print(key)
if all[indexPath.row].id == key.key {
cell.textLabel?.text = all[indexPath.row].id
cell.detailTextLabel?.text = all[indexPath.row].timestampValue
cell.imageView!.image = UIImage(named:"red_icon")
cell.isHidden = false
break
}else{
cell.isHidden = true
}}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var rowHeight:CGFloat = 0.0
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){$0[$1.id] = $1.timestampValue}
for key in listOfStudentsUrl{
if all[indexPath.row].id == key.key{
rowHeight = 49.0
break
}else{
rowHeight = 0.0
}}
return rowHeight
}
So, you are trying to hide some specific cells of your TableView.
One way you could do this is hiding your cell and then setting it's height to zero.
To hide it, use the property .isHidden.
To change it's height, override the function heightForRowAtIndexPath.
See this for more: Hide UITableview cell

IndexPath wrong after array sorting

i get an weird behavior.
Every time I sort my array, my tableView not conforms to my new sorting.
The arrangement is changed but when I select a row the indexpath is not correct.
func SortShoppingListItemsArrayBy_isSelected() -> Void {
if ShoppingListsArray[currentShoppingListIndex].ItemsArray!.count > 0{
ShoppingListsArray[currentShoppingListIndex].ItemsArray!.sort{ !$0.isSelected! && $1.isSelected! }
ShoppingListDetailTableView.reloadData()
}
}
Does anybody can tell me why?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if ShoppingListsArray[currentShoppingListIndex].ItemsArray!.count > 0 {
let row = indexPath.row
//Allow only select on checked items => unchecked must be dropped to basket
if ShoppingListsArray[currentShoppingListIndex].ItemsArray![row].isSelected == false { return }
ShoppingListsArray[currentShoppingListIndex].ItemsArray![row].isSelected = ShoppingListsArray[currentShoppingListIndex].ItemsArray![row].isSelected == false ? true : false
firebaseShoppingListItem.EditIsSelectedOnShoppingListItem(list: ShoppingListsArray[currentShoppingListIndex], shoppingListItem: ShoppingListsArray[currentShoppingListIndex].ItemsArray![row])
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String.ShoppingListItemTableViewCell_Identifier, for: indexPath) as! ShoppingListItemTableViewCell
cell.selectionStyle = .none
cell.ConfigureCell(shoppingListItem: ShoppingListsArray[currentShoppingListIndex].ItemsArray![indexPath.row])
return cell
}

Array of objects and Table View

I have an Array of objects like this:
var Dogs : [Dog] = [Dog]()
To give you an example it looks like this when you print it :
[Dog {
name = Dog1;
age = 9;
}, Dog {
name = Dog2;
age = 4;
}]
I want to show this element in a Table View so I use this classical function :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.titleLabel.text = Dogs[indexPath.row]["name"]! as? String
return cell
}
But I get an ERROR :
fatal error: Array index out of range
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let dog = Dogs[indexPath.row]
cell.titleLabel.text = dog.name
return cell
}

Resources