user3521459
user3521459

Reputation: 3

How to reset tableViewCell's content in iOS?

I create custom cell and for some cell I add new subView:

  static NSString *cellIdentifierCell = @"Cell";

FeedCell *cell = (FeedCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (!cell)
{
    cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierCell]; //frame
}

 //here add subView

     if (wallPost.attachmentLinks[@"audio"])
{
    NSLog(@"%ld",(long)indexPath.row);
    [cell addAudio:wallPost.attachmentLinks[@"audio"] fromStartFrame:CGRectMake(10, 50 + collectionHeight + 5 + 90 + 5, 300, 20)];
   } else {
    for (UIView *v in cell.subviews)    //// (1)
    {
        if (v.tag == 333)
        {
            [v removeFromSuperview];
        }
    }
}

Here is the cell method I use:

 - (void)addAudio:(NSArray *)arrayAudio fromStartFrame:(CGRect)frame
{
for (NSDictionary *dic in arrayAudio)
 {
    UIView *audioView = [[UIView alloc]init];
    audioView.frame = CGRectMake(10, frame.origin.y-100, 300, 20);
    audioView.backgroundColor = [UIColor yellowColor];
    audioView.tag = 333;
    [self addSubview:audioView];

}
[self layoutSubviews];
 }

Problem is that I have subViews in cells that must not have it. Part (1) does not run. How can I reset cell or delete custom View from it?

Upvotes: 0

Views: 169

Answers (2)

msmq
msmq

Reputation: 1328

It would be better if you create two cell views. Its not best approach to remove views by using tags. on else portion of your code, just use alternate view that will give look as empty view. It may be the alternate approach to handle the situation.

Upvotes: 1

user3386109
user3386109

Reputation: 34839

You should always remove the old view, before adding the new view. So the code should look like this

for (UIView *v in cell.subviews)    //// (1)
{
    if (v.tag == 333)
        [v removeFromSuperview];
}

if (wallPost.attachmentLinks[@"audio"])
{
    NSLog(@"%ld",(long)indexPath.row);
    [cell addAudio:wallPost.attachmentLinks[@"audio"] fromStartFrame:CGRectMake(10, 50 + collectionHeight + 5 + 90 + 5, 300, 20)];
}

Upvotes: 0

Related Questions