简体   繁体   English

Swift Tableview获取特定的单元格不起作用

[英]Swift Tableview get specific cell not working

I'm trying to clear a specific cell for my tableview. 我正在尝试为表格视图清除特定的单元格。 I know to achieve that by using this code right here: 我知道可以通过在此处使用以下代码来实现:

let index = IndexPath(row: 1, section: 0)
if let cell = self.tableView.cellForRow(at: index) as? SomeTableViewCell {
     // do sth
  }else {
     print("Row 1 not found")
}

But the problem is when I use this method with a bigger screen phone like Iphone7, 8, X... It'll be working fine but when I tested on iphone5, 6 or the smaller screen that Row 1 is not visible, It'll print that "Row 1 not found" 但是问题是,当我在较大的屏幕电话(例如Iphone7、8,X)上使用此方法时,它可以正常工作,但是当我在iphone5、6或较小的屏幕上进行测试时,第1行不可见,将显示“找不到行1”

Seems like you've almost found the problem yourself! 好像您自己几乎已经发现问题了!

As with tableviews, the iOS reuses cells them to preserve memory and to offer a seamless smooth scrolling experience to the user. 与表格视图一样,iOS重用它们的单元格以保留内存并为用户提供无缝的平滑滚动体验。

The iOS reuses the cells when they disappear from the screen. 当它们从屏幕上消失时,iOS会重用它们。 ie: When you scroll down, the initial cells start to be reused. 即:当您向下滚动时,初始单元格将开始被重用。 In your larger screens, this takes a while to occur as it may be displaying 20 cells at once vs 10 screens (at once) on your smaller screens. 在较大的屏幕上,这需要一段时间,因为它可能一次显示20个单元,而在较小的屏幕上则显示10个屏幕。

If you want to permanently remove this cell, then you should remove the corresponding data object from your model which backs up the table view (datasource) and reload the tableview. 如果要永久删除此单元格,则应从模型中删除相应的数据对象,该对象将备份表视图(数据源)并重新加载表视图。

If you don't want to permanently remove this cell, then you can reset it only when the table actually needs it to be drawn. 如果您不想永久删除此单元格,则只能在表格实际需要绘制它时才能将其重置。 This can be done in the datasource method 这可以在数据源方法中完成

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
            var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
//Do your Resetting code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM