Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't remove label from UITableViewCell

I have a UITableView named "TaskTable" and I am adding a label in the contentview of each cell of the TaskTable in this method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

My label tag is 50 and I am using in built cell of table view for that not custom cell.

now when i try to remove my lable from TaskTable using this code:

for(UILabel *lbl in [cell subviews])
    {
       if(lbl.tag == 50)
        {
          [lbl removeFromSuperview];
        }

   }

The code isn't entering this if condition. Why doesn't it find the label? Is this happening because I am using the built in cell that only find its own text-label, or there is some other issue that I am missing?

like image 966
KDeogharkar Avatar asked Feb 21 '23 12:02

KDeogharkar


1 Answers

You've said you're adding it to the content view of your cell. However your code above is going through the subviews of your cell itself - this only goes one level deep, so it will return the content view, but not the subviews of your content view.

for(UILabel *lbl in [cell.contentView subviews]) 
    { 
       if(lbl.tag == 50) 
        { 
          [lbl removeFromSuperview]; 
        } 

   }

Should work, but really a custom cell subclass with the label as a property would be better.

like image 179
jrturton Avatar answered Mar 08 '23 12:03

jrturton