Swift - 带有按钮的自定义单元格的Tableview

时间:2016-08-19 16:35:06

标签: ios swift tableview

我有一个带有搜索栏的表格视图。最初加载视图时,我看到在空行上显示一个添加按钮。这应该仅在我显示结果时出现。怎么能隐藏这个?我尝试将隐藏属性设置为true但不起作用。

第二部分是我想在按钮上附加一个函数,这样当按下它时会执行代码来添加行中的朋友。以下是我必须支持我的问题的屏幕截图:

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
}

2 个答案:

答案 0 :(得分:1)

重复使用单元格时,不会调用awakeFromNib。 尝试在您的条件中添加其他部分:

class PageController extends ControllerBase
{
    public function initialize(){
        $this->init();
        $this->view->setLayout( 'website' );
    }

    public function indexAction ($url=''){
        if($url == 'about')
            $this->view->pick('page/about');
    }
}

答案 1 :(得分:0)

这只是一种格式的评论,在downvoting之前写评论:

尝试替换:

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
}