Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your main problem here is that you are recreating new views in your cell every time this method is called. You want to create all reusable elements inside the <code>if(cell == nil)</code> otherwise it will make duplicates. Anything that is dynamic must be created outside of this. I took your code and modified it. This should work better.</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { // Everything that does not change should go in here! cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 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 addSubview:pricelabel]; UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom]; [market setFrame:CGRectMake(200, 6, 30, 30)]; [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; [cell addSubview:market]; } // find market button, since we could be reusing a cell we cannot rely on a tag // value to find it. (This would only work with one button though). UIButton *market; for (UIView *subview in cell.subviews) { if ([subview isKindOfClass:[UIButton class]]) { market = (UIButton *)subview; break; } } // set all defaults in case of reuse [market setImage:[UIImage imageNamed:@"DefaultImage.png"] forState:UIControlStateNormal]; market.selected = YES; market.enabled = NO; market.clearsContextBeforeDrawing = NO; if([sellingArray count] &gt; 0) { NSLog(@"sellingArray %@",sellingArray); if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) { // not sure if this is supposed to be YES or NO market.clearsContextBeforeDrawing = YES; [market setSelected:NO]; [market setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; market.enabled = YES; } } [market setTag:indexPath.row]; return cell; } </code></pre> <p>Since it appears you are not using ARC, make sure you look over this code for any needed reference counting.</p>
 

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