简体   繁体   English

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

[英]tableview button creation dynamically in cell issue in swift

I am trying to implement dynamic button on tableview cell .i am able to add the button but as per the requirement on last cell i don't want to add button but its getting added there also as below mention screen capture 我正在尝试在tableview单元上实现动态按钮。我能够添加按钮,但是根据对最后一个单元格的要求,我不想添加按钮,但它也被添加到了那里,如下所述

在此处输入图片说明

This is the requirement my cellForRowAt indexPath code is below. 这是我的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!
}

and the result after this show below screen capture 屏幕下方显示此结果,结果如下

在此处输入图片说明

i don't want to display the delete button in +Add Room row which is present in last row of table. 我不想在表格的最后一行显示的+ Add Room行中显示删除按钮。 Please suggest me how to get the out as mention in the requirement . 请建议我如何如要求中所述。 i made a condition for the text ("Add Room Text") to shown in centre and successfully displaying in center. 我为文本(“添加会议室文本”)显示在中间并成功显示在中间创造了条件。 Thanks in advance please help me to solve this issue 预先感谢,请帮助我解决此问题

2nd issue 第二期

in this given list if i select "COPENHAGEN " and try to delete this , its deleting the upper content means BARCELONA file gets deleted. 在此给定列表中,如果我选择“ COPENHAGEN”并尝试将其删除,则删除上面的内容意味着BARCELONA文件被删除。 i am not delete same seleted content from list . 我不是从列表中删除相同的选定内容。

It happens because UITableView reuses previously created cells. 发生这种情况是因为UITableView重用了先前创建的单元格。 So for the last cell it takes already created cell with button inside. 因此,对于最后一个单元格,它需要使用已创建的带有内部按钮的单元格。

It would be better to create your own subclass of UITableViewCell and use it. 最好创建自己的UITableViewCell子类并使用它。

But if you don't want to do that you can use this version of your code: 但是,如果您不想这样做,则可以使用以下版本的代码:

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!
}

Try this way 试试这个

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)
    }

I have not tested this above code just made some minor changes maybe it will work :) If not so please let me know. 我没有测试上面的代码,只是做了一些细微的更改,也许它可以工作:)如果没有,请告诉我。

EDIT1: Answer updated EDIT1:答案已更新

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!
}

Try this. 尝试这个。 It is because of your program flow. 这是因为您的程序流程。 btn instance is created for every table rows and you set hidden just the instance of the btn for last row. 将为每个表行创建btn实例,并且仅将btn的实例隐藏设置为最后一行。 Table cell reused dequeue cell even though you did not add created btn into table cell content view for the latest cell. 表单元重用了出队单元,即使您没有将创建的btn添加到最新单元的表单元内容视图中。 So, you need to search the button from the content view and set hidden. 因此,您需要从内容视图中搜索按钮并设置为隐藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM