xcode - Swift 2 tableview 滚动更改数据

标签 xcode swift tableview

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

这是来自 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
}}

最佳答案

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

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
}

这将防止滚动到屏幕外的重复使用单元格的按钮出现在缺少电话号码的单元格中。

关于xcode - Swift 2 tableview 滚动更改数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35953138/

相关文章:

swift - 输入一个节点,它就会消失 Arkit

ios - 如何调试 'fatal error: unexpectedly found nil while unwrapping an Optional value'

ios - 动画 UITextView 导致动画时文本消失

ios - UIView.frame 不正确

iOS UITableView 正在绘制奇怪的单元格

ios - 总是将除一个对象之外的两个对象添加到数组中(否则错误)

ios - 如何为分组样式 TableView 的给定部分设置固定高度?

ios - 从 Xcode 构建更快的 "release"?

ios - 如何使用 Storyboard 自动布局调整这两个元素并调整其大小?

ios - Xcode:使用 pod 依赖项创建框架