Swift,防止 uitableviewcell 重用

标签 swift tableview reusability uitableview

如何解决 TableView 数据重叠问题? 我搜索了一些对象 C 版本的答案,但它似乎不适用于 Swift。我试图获取 cell==nil,但它给了我一个错误

      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let current_patient = patients[indexPath.row]
    let cell = myTable.cellForRowAtIndexPath(indexPath) as! PatientTableViewCell
    if (cell){
        let cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
    }
    let cell = myTable.dequeueReusableCellWithIdentifier("patientCell", forIndexPath: indexPath) as! PatientTableViewCell
    if(cell != nil){
        var subviews = cell.contentView.subviews
        subviews.removeAll()
    }
   //configure cell
   let type = current_patient.enrollable_type
   cell.patientTypelbl.text = type
   return cell
   }

折腾了两天,终于明白了。解决这个问题的关键是找到哪个小区被恢复了。我现在正在做的是给单元格一个 var 标识符。

    class PatientTableViewCell: UITableViewCell {

@IBOutlet weak var followupBtn: UIButton!
@IBOutlet weak var viewBtn: UIButton!
@IBOutlet weak var titleType: UILabel!
@IBOutlet weak var titleID: UILabel!
@IBOutlet weak var patientTypelbl: UILabel!
@IBOutlet weak var patientIDlbl: UILabel!

**var identifier = false**
    }

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let current_patient = patients[indexPath.row]

    var cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
           if(cell.identifier == true){
        cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
    }
    //config cell
    cell.identifier = true   //important

我希望它可以帮助别人。 :)

最佳答案

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let current_patient = patients[indexPath.row]
    let cell: PatientTableViewCell! = tableView.dequeueReusableCellWithIdentifier("patientCell") as? PatientTableViewCell

    //configure cell
    let type = current_patient.enrollable_type
    cell.patientTypelbl.text = type
    return cell
} 

关于Swift,防止 uitableviewcell 重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37358857/

相关文章:

ios - Swift,当 Core Animation 完成时

ios - Swift IOS 在弹出后保持 View Controller 在后台运行

swift - 对成员 'dataTask(with:completionHandler:)' swift 3.0 的模糊引用

json - Swift 3 XCode 8.3.3 XML 解析

javafx - CSV 到 TableView 跳过标题 (JavaFX)

python - Golang 测试中的 fixture

java - 在 NetBeans GUI 设计器中重用 JPanel

ios - TableView 上的搜索栏不起作用

ios - swift : Switching tableview cells in cellForRowAtIndexPath

python - 使用 gtkglade 构建可重用组件