UITableViewCell 每 5 个单元格重复一次(在标记为重复之前,请阅读整个任务)
UITableViewCell is repeating after every 5th cell (Before you mark as duplicate kindly read the whole quest )
(在你标记为重复之前,你必须阅读整个问题,我发布这个是因为我没有找到相关和适当的解决方案也需要 swift 中的解决方案)
我已经创建了一个演示项目,并从自定义单元格上的数组中加载并显示了名称和区域。
我注意到在每第 5 个单元格之后意味着第 6 行重复第 0 个单元格的内容
例如
演示代码如下
class demoTableCell: UITableViewCell {
@IBOutlet var name : UILabel!
@IBOutlet var area : UILabel!
}
extension ViewController:UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrDemo.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! demoTableCell
cell.name.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Name") as? String
cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String
if indexPath.row == 0{
cell.name.isHidden = true
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
因为我隐藏了第 0 个单元格上的第一个标签,所以我发现第 6 行也受到第 0 个单元格的已实现功能的影响。这意味着还要隐藏每第 6 个单元格的 label1,因为我附上了下面的屏幕截图,这样您就可以得到确切的问题(只有 table 视图可滚动时才会发生此问题)
因为我也尝试从 this link 解决这个问题,但面临同样的问题,找不到合适的解决方案,我被困在这里。
记住 - 细胞被重复使用。
您隐藏了单元格,但从未明确取消隐藏单元格
当您来到第 6 行时,您正在重新使用第 0 行的单元格,并且 isHidden = true
您需要做的就是扩展您的检查,隐藏您需要隐藏的行,并明确显示您需要查看的单元格。如果您还添加了移动横幅 - 您还需要检查它是否已加载,如果不需要则将其删除。请记住 - 它可能不是第 6 行 - 这就是它在当前屏幕尺寸
下的工作方式
如果您要使用的单元格之间确实存在显着差异,您最好使用两个不同的 类 - 这样您就不必考虑隐藏标签
class demoTableCell: DemoTableCellNormalRow {
@IBOutlet var name : UILabel!
@IBOutlet var area : UILabel!
}
class demoTableCell: DemoTableCellFirstRow {
@IBOutlet var area : UILabel!
@IBOutlet var movingBannerView : LCBannerView!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
if row == 0 {
var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! DemoTableCellFirstRow
cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String
// populate the bannerview which already exists, or create a new one
return cell
} else {
var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! DemoTableCellNormalRow
cell.name.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Name") as? String
cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String
return cell
}
}
单元格被重复使用,您必须确保每个 UI 元素都设置为定义的状态。
您正在使用 if
子句,但没有 else
大小写或默认值。
简单的解决方案:
直接替换
if indexPath.row == 0 {
cell.name.isHidden = true
}
和
cell.name.isHidden = indexPath.row == 0
这会将 hidden
属性 始终设置为定义的状态。
和通常的不一样
- 不要在 Swift 中使用
NSDictionary
。
- 不要
valueForKey
除非你真的需要 KVC(实际上在这里你不需要)。
在您的单元格中实施 prepareForReuse class
override func prepareForReuse() {
name.isHidden = false
}
(在你标记为重复之前,你必须阅读整个问题,我发布这个是因为我没有找到相关和适当的解决方案也需要 swift 中的解决方案)
我已经创建了一个演示项目,并从自定义单元格上的数组中加载并显示了名称和区域。 我注意到在每第 5 个单元格之后意味着第 6 行重复第 0 个单元格的内容 例如
演示代码如下
class demoTableCell: UITableViewCell {
@IBOutlet var name : UILabel!
@IBOutlet var area : UILabel!
}
extension ViewController:UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrDemo.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! demoTableCell
cell.name.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Name") as? String
cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String
if indexPath.row == 0{
cell.name.isHidden = true
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
因为我隐藏了第 0 个单元格上的第一个标签,所以我发现第 6 行也受到第 0 个单元格的已实现功能的影响。这意味着还要隐藏每第 6 个单元格的 label1,因为我附上了下面的屏幕截图,这样您就可以得到确切的问题(只有 table 视图可滚动时才会发生此问题)
因为我也尝试从 this link 解决这个问题,但面临同样的问题,找不到合适的解决方案,我被困在这里。
记住 - 细胞被重复使用。
您隐藏了单元格,但从未明确取消隐藏单元格
当您来到第 6 行时,您正在重新使用第 0 行的单元格,并且 isHidden = true
您需要做的就是扩展您的检查,隐藏您需要隐藏的行,并明确显示您需要查看的单元格。如果您还添加了移动横幅 - 您还需要检查它是否已加载,如果不需要则将其删除。请记住 - 它可能不是第 6 行 - 这就是它在当前屏幕尺寸
下的工作方式如果您要使用的单元格之间确实存在显着差异,您最好使用两个不同的 类 - 这样您就不必考虑隐藏标签
class demoTableCell: DemoTableCellNormalRow {
@IBOutlet var name : UILabel!
@IBOutlet var area : UILabel!
}
class demoTableCell: DemoTableCellFirstRow {
@IBOutlet var area : UILabel!
@IBOutlet var movingBannerView : LCBannerView!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
if row == 0 {
var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! DemoTableCellFirstRow
cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String
// populate the bannerview which already exists, or create a new one
return cell
} else {
var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! DemoTableCellNormalRow
cell.name.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Name") as? String
cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String
return cell
}
}
单元格被重复使用,您必须确保每个 UI 元素都设置为定义的状态。
您正在使用 if
子句,但没有 else
大小写或默认值。
简单的解决方案:
直接替换
if indexPath.row == 0 {
cell.name.isHidden = true
}
和
cell.name.isHidden = indexPath.row == 0
这会将 hidden
属性 始终设置为定义的状态。
和通常的不一样
- 不要在 Swift 中使用
NSDictionary
。 - 不要
valueForKey
除非你真的需要 KVC(实际上在这里你不需要)。
在您的单元格中实施 prepareForReuse class
override func prepareForReuse() {
name.isHidden = false
}