如何从 Controller View 中删除各个部分?我在标题中有一个按钮,所有内容都已连接。只是不确定如何编写 3 个不同部分的代码。
我的数据模型
var fire = [UIImages]
var water = [UIImages]
var air = [UIImages]
var fireLabel = [String]
var waterLabel = [String]
var airLabel = [String]
我的单元配置代码
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell
cell.fire.image = fireImages[indexPath.row]
cell.fireLabel.text = fireNames[indexPath.row]
return cell
} else if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell
cell.water.image = waterImages[indexPath.row]
cell.waterLabel.text = waterNames[indexPath.row]
return cell
} else {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell
cell.air.image = airImages[indexPath.row]
cell.airLabel.text = airNames[indexPath.row]
return cell
}
}
这是我的按钮代码,它位于每个标题中。我想要做的是,当您单击此按钮时,它会删除整个部分。再次为每个。但我似乎无法让它发挥作用。
//Delete Section Button
@IBAction func deleteSectionButton(sender: UIButton) {
//Section tag
let section = sender.tag
//Update data model
fireImages.removeAtIndex(section)
fireNames.removeAtIndex(section)
self.collectionView?.deleteSections(NSIndexSet(index: section))
}
我收到此错误:
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:无效的节数。更新后 Collection View 中包含的节数 (3) 必须等于更新前 Collection View 中包含的节数 (3),加上或减去插入或删除的节数(0 个插入,1 个已删除)。'
但是我不知道这意味着什么。
最佳答案
您可以在更新数据后重新加载 Collection View 。它将按比例将您的数据填充回 Collection View
self.collectionView.reloadData();
关于swift - 如何删除 Collection View 中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38909710/