Ankita Khare
Ankita Khare

Reputation: 15

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

enter image description here

This is the requirement my cellForRowAt indexPath code is below.

 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

enter image description here

i don't want to display the delete button in +Add Room row which is present in last row of table. 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. i am not delete same seleted content from list .

Upvotes: 0

Views: 1110

Answers (3)

StuckPixel
StuckPixel

Reputation: 111

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. Table cell reused dequeue cell even though you did not add created btn into table cell content view for the latest cell. So, you need to search the button from the content view and set hidden.

Upvotes: 0

Konstantin Simakov
Konstantin Simakov

Reputation: 166

It happens because UITableView reuses previously created cells. 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.

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

Upvotes: 1

user9106403
user9106403

Reputation:

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

Upvotes: 0

Related Questions