Swift 为什么我的最后一个单元格失去了背景色和内容
以下是我的代码,我无法理解为什么我会丢失最后一个单元格的功能:Swift 为什么我的最后一个单元格失去了背景色和内容,swift,uitableview,Swift,Uitableview,以下是我的代码,我无法理解为什么我会丢失最后一个单元格的功能: extension ItemsViewController { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "UITabl
extension ItemsViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
let item = itemStore.allItems[indexPath.section]
if indexPath.row == item.count {
let lastCell = UITableViewCell(style: .default, reuseIdentifier: "lastCell")
lastCell.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
lastCell.textLabel?.text = "No more Items"
return lastCell
}
cell.textLabel?.text = item[indexPath.row].name
cell.detailTextLabel?.text = "$\(item[indexPath.row].valueInDollars)"
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return section == 0 ? "below 50" : "above 50"
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemStore.allItems[section].count + 1
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return indexPath.row == itemStore.allItems[indexPath.section].count - 1 ? 44 : 60
}
}
如果不使用最后一种方法(heightForRowAt
),则一切正常。但如果我使用它,我会得到正确的单元格大小,但最后一个单元格的内容和颜色也会丢失。乍一看
if indexPath.row == item.count {
我相信错误可能就在那里。打印出每一个的计数。对于indexPath,您可能会少得到1。因此,不会设置最后一个内容
还没有运行代码 我认为您需要注册
lastCell
,然后使用dequeueReusableCell加载传感器,以下是代码:
if indexPath.row == item.count {
let lastCell = UITableViewCell(style: .default, reuseIdentifier: "lastCell")
lastCell.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
lastCell.textLabel?.text = "No more Items"
return lastCell
}
替换为:
tableView.register(UITableViewCell.self, forCellReuseIdentifier:"lastCell")
if indexPath.row == item.count {
let lastCell = tableView.dequeueReusableCell(withIdentifier: "lastCell", for: indexPath)
lastCell.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
lastCell.textLabel?.text = "No more Items"
return lastCell
}
我有一个正确的答案。使用以下
cellForRow
实现代替您的实现:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Do not dequeue the cell here, because two lines below you might decide that you do not want to use it
// let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
let item = itemStore.allItems[indexPath.section]
if indexPath.row == item.count {
let lastCell = UITableViewCell(style: .default, reuseIdentifier: "lastCell")
lastCell.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
lastCell.textLabel?.text = "No more Items"
return lastCell
}
// Dequeue reusable cell here, when you know you are going to use it:
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
cell.textLabel?.text = item[indexPath.row].name
cell.detailTextLabel?.text = "$\(item[indexPath.row].valueInDollars)"
return cell
}
唯一的区别是,只有在测试之后,只有当您知道将要使用退出队列的单元时,才将可重用单元退出队列。Tableview显然希望您返回该单元格(如果您已经将其退出队列)。它可能会使用它来提高渲染效率,甚至在您真正返回单元格(并且只修改您稍后更改的内容)之前就渲染单元格,因此最后一个单元格的代码似乎不起作用(它起作用,但在您返回最后一个单元格之前,表会渲染出队列的单元格)
这个故事的寓意是:如果您为给定的indexPath将可重用单元出列,tableView希望您也返回它
官员:
如果现有单元格可用,此方法将其出列,或者根据以前注册的类或nib文件创建一个新单元格,并将其添加到表中。
如果要为指定的indexPath对单元格进行排队,则已将其添加到表中。这意味着,如果使用dequeueReusableCell(带标识符:)
而不是dequeueReusableCell(带标识符:)
,那么调用顺序就无关紧要了,因为dequeueReusableCell(带标识符:)
不会为您将其添加到表中。但是,我还是希望使用我的解决方案,只有在您需要时才将其出列。尝试以下方法:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == item.count {
// Place your desired color for the last cell.
cell.backgroundColor = UIColor.red
}
else {
// This should be the normal cell background color.
cell.backgroundColor = UIColor.white
}
}
对于您的需求,一个可重复使用的单元就足够了。假设您已经注册了标识符为“MyTableViewCellIdentifier”的单元格。
请参见以下方法:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCellIdentifier", for: indexPath)
let item = itemStore.allItems[indexPath.section]
if indexPath.row == item.count {
cell.textLabel?.text = "No more Items"
cell.detailTextLabel?.text = nil
}
else {
cell.textLabel?.text = item[indexPath.row].name
let valueInDollars = item[indexPath.row].valueInDollars
cell.detailTextLabel?.text = $ + valueInDollars
}
return cell
}
(Swift 3)如果您所做的只是将一个单元格添加到最后一个索引中,并指示它是tableView列表的底部,那么为什么不简单地使用ViewForFooterInstitution并向其添加标签呢?为什么返回
return itemStore.allItems[section].count+1
?这应该返回实际计数:return itemStore.allItems[section].count
。唯一奇怪的是indepath.row==itemStore.allItems[indepath.section]。计数-1?44:60
-我猜您想要:indexPath.row==itemStore.allItems[indexPath.section]。计数?44:60
,但是,这绝对不会导致您提到的问题。这正是我的第一个想法。但是,如果您检查numberOfRows,他返回的是item.count+1,因此应该是fine,它仍然会不同步,因为索引从0开始。所以你有索引[0,1,2,3,4]=5和item.count=[1,2,3,4,5]+1=6,例如,相信我,这很好。item.count+1是表中的行数。。if测试跳过了最后一个索引,并将其用于最后一个单元格。如果indexath.row+1==item.count{那么?不,这很好。indexath.row==item.count。索引为6的行是第七行(因为它从0开始),那么如果这6等于item.count,那么它就是最后一个单元格了。谢谢米兰!对我来说,这是一个美妙的时刻。