[英]Swift - Tableview with custom cell with button
我有一个带有搜索栏的表格视图。 最初加载视图时,我看到在空行上显示了一个添加按钮。 这应该仅在我显示结果时出现。 如何将其隐藏? 我尝试将hidden属性设置为true,但没有用。
第二部分是我想将一个功能附加到按钮,以便在按下按钮时将执行代码以在行中添加朋友。 这是我必须支持我的问题的屏幕截图:
这是我的自定义单元格的代码:
class AddFriendsTableViewCell: UITableViewCell {
@IBOutlet weak var addButton: UIButton!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var usernameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.addButton.hidden = true
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
这是我要填充单元格的地方:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! AddFriendsTableViewCell
if(self.friendUsername != "" && self.friendName != "")
{
cell.nameLabel.text = self.friendName
cell.usernameLabel.text = self.friendUsername
cell.addButton.hidden = false
}
return cell
}
重用单元格时不会调用awakeFromNib。 尝试在您的条件中添加其他部分:
if(self.friendUsername != "" && self.friendName != "") {
cell.nameLabel.text = self.friendName
cell.usernameLabel.text = self.friendUsername
cell.addButton.hidden = false
} else {
cell.addButton.hidden = true
}
这只是带有格式的评论,请在降低投票率之前写评论:
尝试替换:
if(self.friendUsername != "" && self.friendName != "")
与:
if let friendUsername = self.friendUsername, let friendName = self.friendName where !friendUsername.isEmpty && !friendName .isEmpty
正如Max所写,您必须使用以下方法管理else语句:
else {
cell.addButton.hidden = true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.