UITableViewCell with Subviews not reused

I added a simple UITableView screen to my screen. However, I want the cells in it to display a bunch of user interface elements (basically, view and labels). Since UITableViewCell does not give me much opportunity to configure it freely, I decided to add all the elements that I need as cell subzones. Here is my cellForRowAtIndexPath method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 25.0f, 25.0f)]; view1.backgroundColor = [UIColor redColor]; view1.layer.shadowColor = [[UIColor blackColor] CGColor]; view1.layer.shadowOffset = CGSizeMake(0.0f, 0.0f); view1.layer.shadowOpacity = 0.8f; view1.layer.shadowRadius = 3.0f; [cell addSubview:view1]; UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 5.0f, 100.0f, 15.0f)]; label1.text = @"dummy text 1"; label1.backgroundColor = [UIColor clearColor]; label1.textColor = [UIColor lightGrayColor]; label1.textAlignment = UITextAlignmentRight; label1.font = [UIFont systemFontOfSize:12.0f]; label1.lineBreakMode = NSLineBreakByTruncatingTail; label1.shadowOffset = CGSizeMake(0.0f, 1.0f); label1.shadowColor = [UIColor blackColor]; [cell addSubview:label1]; UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 25.0f, 100.0f, 15.0f)]; label2.text = @"dummy text 2"; label2.backgroundColor = [UIColor clearColor]; label2.textColor = [UIColor lightGrayColor]; label2.textAlignment = UITextAlignmentRight; label2.font = [UIFont systemFontOfSize:12.0f]; label2.lineBreakMode = NSLineBreakByTruncatingTail; label2.shadowOffset = CGSizeMake(0.0f, 1.0f); label2.shadowColor = [UIColor blackColor]; [cell addSubview:label2]; UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 45.0f, 100.0f, 15.0f)]; label3.text = @"dummy text 3"; label3.backgroundColor = [UIColor clearColor]; label3.textColor = [UIColor lightGrayColor]; label3.textAlignment = UITextAlignmentRight; label3.font = [UIFont systemFontOfSize:12.0f]; label3.lineBreakMode = NSLineBreakByTruncatingTail; label3.shadowOffset = CGSizeMake(0.0f, 1.0f); label3.shadowColor = [UIColor blackColor]; [cell addSubview:label3]; return cell; } 

All of the codes above work fine, as the cell setup goes on. The problem is that when a cell leaves the visible region, its subitems are not freed. When I move the table up and down, the shadow on the added UIView becomes darker and darker, and I assume the labels are not released either.

How to fix this problem? I assume that I could subclass the UITableViewCell class, but I would just add subviews to the cell class. This is not like a solution. Is there a way to get a cell to release its subitems when it disappears, or is there a reliable way to truly and freely customize a cell?

Thanks!

Some additional information: I do not use IB (I do everything programmatically) I use ARC I use Xcode 4.6 My SDK - iOS 6.1

+4
source share
3 answers

You can make your code work in two ways.

The correct way to subclass and show added views as properties.

the wrong way is to modify your code as follows:

 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } for (UIView *cellView in sell.view) { [cellView removeFromSuperVuew]; } 

You notice that the shadow is getting darker and darker. This is due to the fact that every time you load (and not only when creating) your cell, add (among all other sub-items).

When you decide to use a subtitle, be sure to make an imlement:

 - (NSString *)reuseIdentifier { return @"hereGoesTeuseIdentifierThatYouWillUseForThissKindOfCell"; } 
+2
source

You must subclass UITableViewCell and do all the customization.

+2
source

The problem is that the cells are recycled. I'm thinking about it. You attached all of these UIViews to the cell each time you initialize or reuse the cell. Simply moving the parses will only bind the views during initialization.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 25.0f, 25.0f)]; view1.backgroundColor = [UIColor redColor]; view1.layer.shadowColor = [[UIColor blackColor] CGColor]; view1.layer.shadowOffset = CGSizeMake(0.0f, 0.0f); view1.layer.shadowOpacity = 0.8f; view1.layer.shadowRadius = 3.0f; [cell addSubview:view1]; UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 5.0f, 100.0f, 15.0f)]; label1.text = @"dummy text 1"; label1.backgroundColor = [UIColor clearColor]; label1.textColor = [UIColor lightGrayColor]; label1.textAlignment = UITextAlignmentRight; label1.font = [UIFont systemFontOfSize:12.0f]; label1.lineBreakMode = NSLineBreakByTruncatingTail; label1.shadowOffset = CGSizeMake(0.0f, 1.0f); label1.shadowColor = [UIColor blackColor]; [cell addSubview:label1]; UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 25.0f, 100.0f, 15.0f)]; label2.text = @"dummy text 2"; label2.backgroundColor = [UIColor clearColor]; label2.textColor = [UIColor lightGrayColor]; label2.textAlignment = UITextAlignmentRight; label2.font = [UIFont systemFontOfSize:12.0f]; label2.lineBreakMode = NSLineBreakByTruncatingTail; label2.shadowOffset = CGSizeMake(0.0f, 1.0f); label2.shadowColor = [UIColor blackColor]; [cell addSubview:label2]; UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 45.0f, 100.0f, 15.0f)]; label3.text = @"dummy text 3"; label3.backgroundColor = [UIColor clearColor]; label3.textColor = [UIColor lightGrayColor]; label3.textAlignment = UITextAlignmentRight; label3.font = [UIFont systemFontOfSize:12.0f]; label3.lineBreakMode = NSLineBreakByTruncatingTail; label3.shadowOffset = CGSizeMake(0.0f, 1.0f); label3.shadowColor = [UIColor blackColor]; [cell addSubview:label3]; } return cell; } 

That said ... If you want to make these cells interesting by adding content, you will need to delete them each time from the cell or keep a link to the cell sub-items. Endpoint, don't just delete all submissions if you can help. If the values ​​just change, subclass UITableViewCell and create your own custom class that allows you to set unique view values.

+2
source

All Articles