[英]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.