bangdel
bangdel

Reputation: 2553

remove the subviews from the contentView of UITableViewCell (reset the UITableView)

Any idea on how to reset a UITableView?

I want to display a new set of data at the press of a button and also remove all the subviews from the cell's contentView and refresh them with a new set of subviews. I tried [tableView reloadData] but the data did get refreshed but the subviews added to the contentView of the cells previously persisted.

Upvotes: 16

Views: 34683

Answers (9)

Swan
Swan

Reputation: 305

cell.contentView.subviews.forEach{ $0.removeFromSuperview() }

Upvotes: 3

user7396942
user7396942

Reputation:

you try with replace the content but not the UI, for example to a label replace the text:

class CustomCell : UITableViewCell     {

    var firstColumnLabel = UILabel()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        firstColumnLabel = //create label

        contentView.addSubview(firstColumnLabel)
    }

    func setData(cad: String) {
        firstColumnLabel.text = cad
    }
}

and in your tableSource

 public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell : UITableViewCell?
        cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
            (cell as! CustomCell).setData(cad: "new value")
        return  cell!
    }

Upvotes: 0

SHINTO JOSEPH
SHINTO JOSEPH

Reputation: 387

for subview in cell.contentView.subviews {
    subview.removeFromSuperview()
}

Upvotes: 0

Ankit Goyal
Ankit Goyal

Reputation: 3049

For Swift 3.1

        let theSubviews: Array = (cell?.contentView.subviews)!
        for view in theSubviews
        {
            view.removeFromSuperview()
        }

Upvotes: 2

bangdel
bangdel

Reputation: 2553

I got the answer :)

if ([cell.contentView subviews]) {
    for (UIView *subview in [cell.contentView subviews]) {
        [subview removeFromSuperview];
    }
}

This piece of code will check if the cell's content view has any subviews.
If there are any, it removes them as the for loop iterates!

Upvotes: 25

Giovanny Piñeros
Giovanny Piñeros

Reputation: 583

I know this question is old, but i found a problem with the solution, if you remove one subview from one prototype cell, this view will be removed from all reused cells, this will create a null pointer in the next cell that will be reused.

If you subclass UITableViewCell and remove the subview from there, you will have to do this verification first:

if (mySubView != nil ) {
        mySubView.removeFromSuperview()
    }

This also will work inside your cellForRow at indexPath method

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCustomCell

    if (cell.mySubView != nil) {
       cell.mySubView.removeFromSuperview()
     }
    return cell
}

Whit this you avoid the null pointer, the code is for swift 3. Hope this help somebody. :)

Upvotes: 2

EmptyStack
EmptyStack

Reputation: 51374

Instead of iterating through all the subviews (yourself) to remove them, you can simply do this,

[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

Upvotes: 16

Khaled Annajar
Khaled Annajar

Reputation: 16552

If you want to remove all the subviews of a UITableViewCell you can use this code:

NSArray* subviews = [cell.contentView subviews];
for (UIView* subview in subviews) {
    [subview removeFromSuperview];
}

Using the code of @leftspin

Upvotes: 2

leftspin
leftspin

Reputation: 2488

Better yet, when you create the cell:

#define MY_CUSTOM_TAG 1234
mySubview.tag = MY_CUSTOM_TAG;
[cell.contentView addSubview:mySubview] ;

And later on, when you need to remove it:

[[cell.contentView viewWithTag:MY_CUSTOM_TAG]removeFromSuperview] ;

Upvotes: 39

Related Questions