Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中的单元格问题中动态创建tableview按钮_Ios_Swift_Uitableview - Fatal编程技术网

Ios 在swift中的单元格问题中动态创建tableview按钮

Ios 在swift中的单元格问题中动态创建tableview按钮,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试在tableview单元格上实现动态按钮。我可以添加按钮,但根据最后一个单元格的要求,我不想添加按钮,但它也会添加到那里,如下所示,提及屏幕截图 这是要求 下面是我的cellForRowAt indexPath代码 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableView.dequeueReu

我正在尝试在tableview单元格上实现动态按钮。我可以添加按钮,但根据最后一个单元格的要求,我不想添加按钮,但它也会添加到那里,如下所示,提及屏幕截图

这是要求 下面是我的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!
}
之后的结果将显示在屏幕截图下方

我不想在+添加房间行中显示“删除”按钮,该行位于表的最后一行。 请建议我如何得到的要求中提到的。我设置了一个条件,使文本(“添加房间文本”)显示在中心并成功显示在中心。 提前谢谢,请帮我解决这个问题

第二期

在这个给定的列表中,如果我选择“COPENHAGEN”并尝试删除它,它删除上面的内容意味着巴塞罗那文件被删除。我不会从列表中删除相同的选定内容。

试试这种方法

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)
    }
我还没有测试过上面的代码,只是做了一些小改动,也许它可以工作:) 如果没有,请告诉我

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

试试这个。这是因为你的程序流程。为每个表行创建btn实例,您只为最后一行设置隐藏btn实例。即使未将创建的btn添加到最新单元格的“表单元格内容”视图中,表单元格仍重用了出列单元格。因此,您需要从内容视图中搜索按钮并将其设置为隐藏。

尝试在if-else条件中添加断点,看看它是否位于if?如果indexath.row==(list.count-1){print(“last row”)//在此处添加逻辑}每次单元格出列时,都会向单元格添加一个新按钮。您应该处理这个问题。您只需要在其他部分中使用btn.isHidden=false,只需在UITableViewCell类中而不是CellForRowatineXpath中创建按钮,并根据indexPath隐藏和显示它,同时在prepareForReuse方法中隐藏按钮。@AnkitaKhare如果indexPath.row==(list.count-1),请调试
line返回最后一个案例的
true
false
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!
}