Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly reuse a cell in UITableView
    primarykey
    data
    text
    <p>I'm a newbie. I am using this code to create a <code>UITableViewCell</code> but when I reload the table the button's image is not always correct, although all labels work fine. I don't know why. How can I fix this issue?</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)]; FileNameLabel.tag = 1000; FileNameLabel.backgroundColor = [UIColor clearColor]; FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; FileNameLabel.font = [UIFont boldSystemFontOfSize:16]; FileNameLabel.textColor = [UIColor blackColor]; [cell.contentView addSubview: FileNameLabel]; [FileNameLabel release]; UILabel *UploadTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 150, 25)]; UploadTimeLabel.tag = 2000; UploadTimeLabel.backgroundColor = [UIColor clearColor]; UploadTimeLabel.font = [UIFont fontWithName:@"Helvetica" size:12]; UploadTimeLabel.textColor = [UIColor grayColor]; [cell.contentView addSubview: UploadTimeLabel]; [UploadTimeLabel release]; UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)]; pricelabel.backgroundColor = [UIColor clearColor]; pricelabel.font = [UIFont fontWithName:@"Helvetica" size:16]; pricelabel.font = [UIFont boldSystemFontOfSize:16]; pricelabel.textColor = [UIColor darkGrayColor]; pricelabel.tag = 3000; //pricelabel.hidden = YES; pricelabel.textAlignment = NSTextAlignmentRight; [cell.contentView addSubview: pricelabel]; [pricelabel release]; market = [[UIButton alloc] init];; [market setFrame:CGRectMake(200, 6, 30, 30)]; market.tag = 4000; [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; [cell.contentView addSubview:market]; } if( [temp count] &gt; 0) { UILabel *fileNameLbl = (UILabel*)[cell.contentView viewWithTag:1000]; fileNameLbl.text =[temp objectAtIndex:indexPath.row]; UILabel *uploadlbl = (UILabel*)[cell.contentView viewWithTag:2000]; uploadlbl.text =[UploadTimeAllArr objectAtIndex:indexPath.row]; } UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000]; [marketButton setTag:indexPath.row]; if([sellingArray count]&gt;0) { NSLog(@"sellingArray %@",sellingArray); if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing { [marketButton setSelected:NO]; [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; marketButton.enabled = YES; } else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace { [marketButton setSelected:YES]; [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal]; marketButton.enabled = YES; } } return cell; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload