Mr_*_*sov 0 core-data ios swift
尝试从不同的方式解决这个问题。当前的实现给了我一个错误:“NSInvalidArgumentException”,原因:“索引 0 部分中索引 3 处没有对象”它发生在 AppDelegate 类:UIResponder...
我的代码是
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
configureCell(cell: cell, indexPath: indexPath)
var numberOfItems = self.collectionView(self.collectionView, numberOfItemsInSection: 0)
if (indexPath.row == numberOfItems - 1) {
var addCellButton = UIButton(frame: cell.frame)
addCellButton.setTitle("Add", for: UIControlState.normal)
cell.addSubview(addCellButton)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let sections = controller.sections {
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects + 1
}
return 0
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
if let sections = controller.sections {
return sections.count
}
return 0
}
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
您可以像这样编写它来使两个单独的单元出队:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var collectionView:UICollectionView!
var items = ["Apple", "Banana", "Orange", "Watermelon", "Coconut"]
...
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// ------------------------------------------
// +1 here for the extra add button
// at the bottom of the collection view
// ------------------------------------------
return items.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// --------------------------------
// IndexPath row vs Items Count
// --------------------------------
// [0] = Apple
// [1] = Banana
// [2] = Orange
// [3] = Watermelon
// [4] = Coconut
//
// [5] = special cell
//
// ---------------------------------
let cellID = indexPath.row < items.count ? "normalCell" : "specialCell"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)
setupCell(cell: cell, indexPath: indexPath, type: cellID)
return cell
}
func setupCell(cell: UICollectionViewCell, indexPath: IndexPath, type: String) {
switch(type) {
case "normalCell":
setupFruitCell(cell: cell as! FruitCell, indexPath: indexPath)
case "specialCell":
setupSpecialCell(cell: cell as! SpecialCell, indexPath: indexPath)
default:
break
}
}
func setupFruitCell(cell: FruitCell, indexPath: IndexPath) {
cell.label.text = items[indexPath.row]
}
func setupSpecialCell(cell: SpecialCell, indexPath: IndexPath) {
cell.btnAdd.addTarget(self, action: #selector(addButtonTapped), for: UIControlEvents.touchUpInside)
}
func addButtonTapped(sender: UIButton) {
print("Show UI to add new fruit")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2090 次 |
| 最近记录: |