快速在单元格中动态创建tableview按钮

时间:2019-02-25 05:42:51

标签: ios swift uitableview

我正在尝试在tableview单元上实现动态按钮。我能够添加按钮,但是根据对最后一个单元格的要求,我不想添加按钮,但它也被添加到了那里,如下所述 >

enter image description here

这是必要条件 我的cellForRowAt indexPath代码如下。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    if let _ = cell {
        print("cell is available")
    } else {
        cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    }
    cell?.textLabel?.textAlignment = .left
    cell?.textLabel?.text = list[indexPath.row]
    let btn = UIButton(type: UIButtonType.custom) as UIButton

    if cell?.textLabel?.text == "➕ Add Room" || list[indexPath.row] == "➕ Add Room"{
        btn.isHidden = true
        cell?.textLabel?.textAlignment = .center
    }else{
        btn.frame = CGRect(x: 146, y: 0, width: 20, height: (cell?.frame.height)!)
        btn.addTarget(self, action: #selector(buttonPressed(sender: )), for: .touchUpInside)
        btn.tag = indexPath.row
        btn.setImage(UIImage(named: "delete.png"), for: .normal)
        cellx = list[btn.tag]
        cell?.contentView.addSubview(btn)
    }
    return cell!
}

以及此后的结果显示在屏幕截图下方

enter image description here

我不想在表格的最后一行显示的+ Add Room行中显示删除按钮。 请建议我如何如要求中所述。我为文本(“添加会议室文本”)显示在中间并成功显示在中间创造了条件。 预先感谢,请帮助我解决此问题

第二期

在此给定列表中,如果我选择“ COPENHAGEN”并尝试删除它,则删除上面的内容意味着BARCELONA文件被删除。我不是从列表中删除相同的选定内容。

3 个答案:

答案 0 :(得分:1)

发生这种情况是因为UITableView重用了先前创建的单元格。因此,对于最后一个单元格,它需要使用已创建的带有内部按钮的单元格。

最好创建自己的UITableViewCell子类并使用它。

但是如果您不想这样做,可以使用此版本的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    if let cell = cell {
        print("cell is available")
        // Remove previously created button from reused cell view
        if let button = cell.contentView.subviews.first(where: { (view: UIView) -> Bool in
            view.isKind(of: UIButton.self)
        }) {
            button.removeFromSuperview()
        }
    } else {
        cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    }
    cell?.textLabel?.textAlignment = .left
    cell?.textLabel?.text = list[indexPath.row]

    if indexPath.row == (list.count - 1) {
        cell?.textLabel?.textAlignment = .center
    } else {
        let btn = UIButton(type: UIButtonType.custom) as UIButton
        btn.frame = CGRect(x: 146, y: 0, width: 20, height: (cell?.frame.height)!)
        btn.addTarget(self, action: #selector(buttonPressed(sender: )), for: .touchUpInside)
        btn.tag = indexPath.row
        btn.setImage(UIImage(named: "delete.png"), for: .normal)
        cellx = list[btn.tag]
        cell?.contentView.addSubview(btn)
    }
    return cell!
}

答案 1 :(得分:0)

尝试这种方式

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! cell

 if indexPath.row == (list.count - 1) {
        btn.isHidden = true
        cell?.textLabel?.textAlignment = .center
    }else{
        btn.isHidden = false
        btn.frame = CGRect(x: 146, y: 0, width: 20, height: (cell?.frame.height)!)
        btn.addTarget(self, action: #selector(buttonPressed(sender: )), for: .touchUpInside)
        btn.tag = indexPath.row
        btn.setImage(UIImage(named: "delete.png"), for: .normal)
        cellx = list[btn.tag]
        cell?.contentView.addSubview(btn)
    }

我还没有测试上面的代码,只是做了一些小的更改,也许会起作用:) 如果不是这样,请告诉我。

EDIT1: 答案已更新

答案 2 :(得分:0)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    if let _ = cell {
        print("cell is available")
    } else {
        cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    }
    cell?.textLabel?.textAlignment = .left
    cell?.textLabel?.text = list[indexPath.row]
    let btn = UIButton(type: UIButtonType.custom) as UIButton

    if cell?.textLabel?.text == "➕ Add Room" || list[indexPath.row] == "➕ Add Room"{
        for view in cell?.contentView.subviews as [UIView] {
            if let button = view as? UIButton {
               button.isHidden = true
            }
        }
        cell?.textLabel?.textAlignment = .center
    }else{
        btn.frame = CGRect(x: 146, y: 0, width: 20, height: (cell?.frame.height)!)
        btn.addTarget(self, action: #selector(buttonPressed(sender: )), for: .touchUpInside)
        btn.tag = indexPath.row
        btn.setImage(UIImage(named: "delete.png"), for: .normal)
        cellx = list[btn.tag]
        cell?.contentView.addSubview(btn)
    }
    return cell!
}

尝试一下。这是因为您的程序流程。将为每个表行创建btn实例,并且仅将btn的实例隐藏设置为最后一行。表单元重用了出队单元,即使您没有将创建的btn添加到最新单元的表单元内容视图中。因此,您需要从内容视图中搜索按钮并设置为隐藏。