breno breno - 15 days ago 6
Objective-C Question

Collection view inside a cell sometimes confuses your data

I have a table that in each cell contains a

colectionView
. The table works correctly and it sends to each row an array that should mount the
collectionView
. The problem is that when I scroll the screen the last
collectionView
takes the data from the first
collectionView
that is in the first cell of the table.

This is the code in my table:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BSCCell", for: indexPath) as! BSCCell

let itemsArray = dataArray[indexPath.section].object(forKey: "items") as! NSArray
cell.items = itemsArray
return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
return dataArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let itemsArray = dataArray[section].object(forKey: "items") as? NSArray

if itemsArray!.count > 0 {
return 1
} else {
return 0
}
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let itemsArray = dataArray[indexPath.section].object(forKey: "items") as? NSArray
let numberOfLines = ((itemsArray!.count) % 2 == 0 ? itemsArray!.count : Int(itemsArray!.count + 1)) / 2
let totalHeight:CGFloat = CGFloat(numberOfLines * 120)

return totalHeight
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = BIBlue()

let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 8.0, y: 10.0, width: self.view.frame.width - 50, height: 20.0)
titleLabel.text = dataArray[section].object(forKey: "title") as? String
titleLabel.textColor = UIColor.white
titleLabel.font = UIFont(name:"SegoeUI-Semilight", size: 15)
view.addSubview(titleLabel)

let separator = UIView()
separator.frame = CGRect(x: 0.0, y: 0.0, width: view.frame.width, height: 2.0)
separator.backgroundColor = UIColor.red
view.addSubview(separator)

return view
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40.0
}


This is the code for my
collectionView
:

var items:NSArray = []

override func awakeFromNib() {
super.awakeFromNib()
collView.delegate = self
collView.dataSource = self
collView.register(UINib(nibName: "BSCColl", bundle: Bundle.main), forCellWithReuseIdentifier: "BSCColl")
}

override func prepareForReuse() {
items = []
super.prepareForReuse()
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
return CGSize(width: (self.collView.frame.size.width / 2) - 4, height: 110)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let param = items[indexPath.row] as? NSDictionary
let indicator = param?.object(forKey: "IdIndicador") as? String

let notificationName = Notification.Name("bscNotification")
NotificationCenter.default.post(name: notificationName, object: nil, userInfo:["id":indicator!])
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collView.dequeueReusableCell(withReuseIdentifier: "BSCColl", for: indexPath) as! BSCColl
let param = items[indexPath.row] as? NSDictionary
cell.title.text = param?.object(forKey: "Parametro") as! String?
cell.value.text = param?.object(forKey: "ValorCorrente") as! String?
cell.diferenceValue.text = param?.object(forKey: "ValorPadrao") as! String?
cell.metric.text = param?.object(forKey: "Metrica") as! String?

return cell

}

Answer

In cellForRowAt indexPath:, tell the UICollectionView to reload:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "BSCCell", for: indexPath) as! BSCCell
    let itemsArray = dataArray[indexPath.section].object(forKey: "items") as! NSArray
    cell.items = itemsArray

    cell.collectionView.reloadData() // replace collectionView with the actual name you are using

    return cell
}
Comments