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
source share