Xcode Swift 2表格视图滚动更改数据

Xcode Swift 2表格视图滚动更改数据,xcode,swift,tableview,Xcode,Swift,Tableview,我无法正确加载数据!当我向下滚动并备份时,上面的单元格加载得很好。曾经黑暗的图标会点亮,当它们被触摸时,它们会随机拨打一个号码。这是很奇怪的,因为其他带有电话号码的单元格不会只更改保留为“”的单元格 这是来自ViewController.swift的 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return contactList.count } func

我无法正确加载数据!当我向下滚动并备份时,上面的单元格加载得很好。曾经黑暗的图标会点亮,当它们被触摸时,它们会随机拨打一个号码。这是很奇怪的,因为其他带有电话号码的单元格不会只更改保留为“”的单元格

这是来自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
中设置的视觉内容:

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
}

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

您看到此行为的原因是您的
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
}
这将防止屏幕上滚动的重复使用手机的按钮显示在缺少电话号码的手机中