我有一个表格,每个单元格中都包含一个colectionView
。该表正常工作,它向每一行发送一个应该挂载collectionView
的数组。问题是当我滚动屏幕时,最后一个collectionView
从表格的第一个单元格中的第一个collectionView
获取数据。
这是我表中的代码:
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
}
这是我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
}
答案 0 :(得分:1)
在cellForRowAt indexPath:
中,告诉UICollectionView
重新加载:
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
}
或者,您可以在设置items
时触发重新加载作为副作用。在UITableViewCell
的实施中:
var items = [Any] {
didSet {
collectionView.reloadData() // replace collectionView with the actual name you are using
}
}