简体   繁体   中英

issue in reusing tableview custom cell

My custom cell contains the toggling badges and a label inside that badge (both in xib). Then how can I reuse the cell? If I make the cell.imgBadgeImageView.image = nil and cell.lblBadgeLabel.Text = nil in prepareForReuse method of custom cell both of them will disappear from all the remaining cells (as we are reusing the cells).

Do I need to add the badge and label inside it as a subview to the cell from code? If I do so how can I access the cell badge image view and badge label. I need to access these two because there is a nice animation (for the image view) and text change (for the label).

I am currently making all the cells nil which are not in the view using this delegate method:

-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     if ([tableView.indexPathsForVisibleRows indexOfObject:indexPath] == NSNotFound){
         VBMerchantDealCell *cell = (VBMerchantDealCell *)[tableView cellForRowAtIndexPath:indexPath]; 
         cell = nil;
     }
}

I understand that you want to reuse the cell. I don't know if the approach that you are taking is right, but I do it the way Apple has told me to do it.

a. Set the reuse identifier in the xib. Say "MyCustomCell".

b. Register the cell after your table view allocation using:

self.cellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
[self.tableView registerNib:self.cellNib forCellReuseIdentifier:@"CustomCell"];

c. Get the cell at your ``cellForRowAtIndexPath` using:

NSString *identifier = @"MyCustomCell"; 
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM