【问题标题】:Swift 2 tableview scrolling changes dataSwift 2 tableview 滚动更改数据
【发布时间】:2016-06-27 11:03:32
【问题描述】:

我无法正确加载数据!顶部的单元格加载良好,然后当我向下滚动并备份时,曾经必须电话号码的黑色图标亮起,当它们被触摸时,它们会调用一个随机号码。这很奇怪,因为具有电话号码的其他单元格不会仅更改剩下的等于“”的单元格。

这是来自 ViewController.swift

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return contactList.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell

    let contact = contactList[indexPath.row]

    cell.name.text = "\(contact.name)"

    cell.id.text = "\(phoneFormat(contact.callPhone))"

    if contact.callPhone != "" {

        cell.callButton.setImage(UIImage(named: "phone.png"), forState: UIControlState.Normal)

        cell.callButton.userInteractionEnabled = true

        cell.callButton.tag = indexPath.row

        cell.callButton.addTarget(self, action: "call:", forControlEvents: .TouchUpInside)

    }

   /* if contact.smsPhone != nil {

        cell.smsButton.setImage(UIImage(named: "chat.png"), forState: UIControlState.Normal)

        cell.smsButton.userInteractionEnabled = true

        cell.smsButton.tag = indexPath.row
    }

    if contact.email != nil {

        cell.emailButton.setImage(UIImage(named: "email.png"), forState: UIControlState.Normal)

        cell.emailButton.userInteractionEnabled = true

        cell.emailButton.tag = indexPath.row

    }*/

    return cell
}

这是 tableviewcell.swift 文件

class TableViewCell: UITableViewCell {

@IBOutlet var name: UILabel!

@IBOutlet var id: UILabel!

@IBOutlet var callButton: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}}

【问题讨论】:

    标签: xcode swift tableview


    【解决方案1】:

    您看到此行为的原因是您的 if 缺少一个 else 分支,您可以在其中清除 if 中设置的视觉内容:

    if contact.callPhone != "" {
        cell.callButton.setImage(UIImage(named: "phone.png"), forState: UIControlState.Normal)
        cell.callButton.userInteractionEnabled = true
        cell.callButton.tag = indexPath.row
        cell.callButton.addTarget(self, action: "call:", forControlEvents: .TouchUpInside)
    } else {
        cell.callButton.setImage(nil, forState: UIControlState.Normal)
        cell.callButton.userInteractionEnabled = false
        cell.callButton.tag = 0
        cell.callButton.hidden = true
    }
    

    这将防止重复使用的按钮从屏幕上滚出的单元格显示在缺少电话号码的单元格中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      相关资源
      最近更新 更多