2016-08-19 115 views
0

我有一个带有搜索栏的表格视图。当视图最初加载时,我看到在一个空行上显示了一个添加按钮。这应该只在我显示结果时出现。这如何隐藏起来?我试着将隐藏属性设置为true,但没有奏效。Swift - 带按钮的自定义单元格的Tableview

第二部分是我想为按钮附加一个函数,以便按下它时将执行代码以添加行中的朋友。下面是我要支持我的问题截图:

initial view where button should not show

after searching where I want to attach a function to the button

这里是我的自定义单元格的代码:

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 
} 

回答

0

它只是交流omment与格式,downvoting之前写的评论:

尝试更换:

if(self.friendUsername != "" && self.friendName != "") 

有:

if let friendUsername = self.friendUsername, let friendName = self.friendName where !friendUsername.isEmpty && !friendName .isEmpty 

和马克斯写道,你有管理else语句:

else { 
    cell.addButton.hidden = true 
} 
1

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 
} 
相关问题