Ios UITableViewCell中的UIButton标题在重用时更改
我有一个表视图,其中包含一个由电话联系人和facebook好友组成的数据。如果用户是联系人,则手机按钮标题应为“邀请”;如果用户是facebook好友,则手机按钮标题应为“添加” 但是,每个按钮上的标题都是错误的,并且在滚动上交换 这是我的模型:Ios UITableViewCell中的UIButton标题在重用时更改,ios,uitableview,uibutton,swift4,Ios,Uitableview,Uibutton,Swift4,我有一个表视图,其中包含一个由电话联系人和facebook好友组成的数据。如果用户是联系人,则手机按钮标题应为“邀请”;如果用户是facebook好友,则手机按钮标题应为“添加” 但是,每个按钮上的标题都是错误的,并且在滚动上交换 这是我的模型: struct MyContact { var name: String var phone: String var photoUrl: String var isFBUser: Bool } 模型阵列 var myContac
struct MyContact {
var name: String
var phone: String
var photoUrl: String
var isFBUser: Bool
}
模型阵列
var myContacts = [MyContact]()
cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexpath: Indexpath) -> UITableviewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ContactCell
let contact = myContacts[indexPath.row]
if contact.isFBUser == true {
cell.button.setTitle("Add", for: .normal)
} else {
cell.button.setTitle("Invite", for: .normal)
}
cell.contact = contact
cell.configureCell()
}
斯威夫特
func configureCell() {
if contact.isFBUser == true {
button.setTitle("Add", for: .normal)
} else {
button.setTitle("Invite", for: .normal)
}
}
我甚至试着在PrepareForuse中将按钮标题设置为零
override func prepareForReuse() {
super.prepareForReuse
button.setTitle(nil, for: .normal)
}
单元格对象是重复使用的,因此您需要在将它们出列(或将其传递给
configureCell()
)时继续设置它们的contact属性。您的代码似乎正确,但是否可以显示cellforrowatinex
?在调用configureCell()
之前,是否在单元格上设置了联系人
?另外,按钮是如何创建的?我已经更新了我的代码。如果您正在传递一个联系人对象,并且正在调用configureCell()
,那么您不需要将if contact.isFBUser==true{button.setTitle(“Add”,for:.normal)}else{button.setTitle(“Invite”,for:.normal)}/code>作为出列单元格代码?没有“myCell.contact=someArray[indexpath.row]”?你的方法没有返回单元格?对不起,我又更新了我的代码。如果你不介意的话,你能给我举个例子吗?我很困惑,因为我认为我在cellForRowAt中的代码已经在做了?属性的设置一直在进行。