问题描述
限时送ChatGPT账号..我无法正确加载数据!然后当我向下滚动并备份时,顶部的单元格加载良好,必须点亮电话号码的曾经黑暗的图标会亮起,当它们被触摸时,它们会呼叫一个随机号码.这很奇怪,因为具有电话号码的其他单元格不会只更改剩下等于"的单元格.
I am having trouble getting the data to load correctly! The top cells load well then when I scroll down and back up the once dark icons that had to phone number are lit and when they are touched they call a random number. This is odd, as the other cells with a phone number do not change only the ones that are left equalling "".
这是来自 ViewController.swift
This is from the 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 文件
this is the tableviewcell.swift file
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
中设置的视觉内容:
The reason that you see this behavior is that your if
is missing an else
branch, in which you clear out the content of visuals being set in the 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
}
这将防止从屏幕滚动的重复使用的单元格中的按钮显示在缺少电话号码的单元格中.
This would prevent buttons from reused cells scrolled off the screen from showing up in cells for which the phone number is missing.
这篇关于Swift 2 tableview 滚动更改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论