用dequeueReusableCellWithIdentifier ios将单元格中的UImageView绑定到错误的行号

我有UITableView与包含白色图像的自定义单元格 – 当你按下它变成绿色(客户把东西放在他们的篮子里)。

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int volume = self.seats.count; return volume; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { _currentSeat = [_seats objectAtIndex:indexPath.row]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = @"seatCell"; //todo try fix bug with dequing indexes UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } Seat *cur = [_seats objectAtIndex:indexPath.row]; _currentSeat = cur; UILabel* tr = (UILabel*)[cell viewWithTag:51]; tr.text = cur.rowNum; UILabel* tr2 = (UILabel*)[cell viewWithTag:52]; tr2.text = cur.seatNum; NSArray* piecesOfPrice = [cur.price componentsSeparatedByString: @","]; if (piecesOfPrice.count > 1) { cur.price = [piecesOfPrice objectAtIndex:0]; } UILabel* tr3 = (UILabel*)[cell viewWithTag:53]; NSMutableString *prm = [cur.price mutableCopy]; [prm appendString:@" p"]; tr3.text = prm; UITapGestureRecognizer *singleImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addToBasketTapDetected:)]; singleImageTap.numberOfTapsRequired = 1; UIImageView* imv = (UIImageView*)[cell viewWithTag:54]; [imv setUserInteractionEnabled:YES]; [imv addGestureRecognizer:singleImageTap]; imv.tag = indexPath.row; return cell; } -(void)addToBasketTapDetected: (UIGestureRecognizer *)sender{ NSLog(@"single Tap on imageview"); UIImageView *theTappedImageView = (UIImageView *)sender.view; NSInteger tag = theTappedImageView.tag; Seat* seat = [_seats objectAtIndex:tag]; NSMutableString *urlStr = [NSMutableString new]; [ urlStr appendString:@"http://19-00.ru/is-bin/INTERSHOP.enfinity/WFS/1900-1900channel-Site/ru_RU/-/RUR/ViewStandardCatalog-AddProductToCart?SelectedGood=1@"]; [urlStr appendString: seat.productRefID]; NSString *u = [[urlStr mutableCopy]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:u]; NSData *data = [NSData dataWithContentsOfURL:url]; NSString *ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"Row number from sender: %d", tag); if ([ret containsString : seat.productRefID]){ //Ticket_added NSString *thePath = [[NSBundle mainBundle] pathForResource:@"rectangle_21_copy_5" ofType:@"png"]; UIImage *prodImg = [[UIImage alloc] initWithContentsOfFile:thePath]; [theTappedImageView setImage:prodImg]; } else { //server error } } 

ios在每一部分中重复着色每一行中相同数字的图像。 所以我得到多个产品添加到购物篮1点击是不正确的。 方法numberOfRowsInSection返回正确的行数(50)。 我试图玩弄,发现方法didSelectRowAtIndexPath返回正确的行号(前为14)。 但我需要点击图像,而不是在行。 和方法cellForRowAtIndexPath – 是我可以将图像绑定到单元格的唯一位置,但是它与行/分区号码相关。 如何处理这个?

addToBasketTapDetected方法中,您并没有向我们展示如何实际findtheTappedImageView ,我怀疑这是错误的。

但是在任何情况下,我都不会在表格视图控制器中完成它,而是在UIViewCell子类中进行处理,因为你可以确保你有正确的单元格。

另外,我不会使用UIImageView ,而是一个UIButton ,在其中configuration两种状态: UIControlStateNormalUIControlSelected ,当button被轻敲时,将其设置为button.selected = YES (如果再次轻敲,则为NO )。 这会给你一个切换button。

 @interface TableViewController () <TableViewCellDelegate> @property (nonatomic, strong) NSMutableArray *selectedIndexPathes; @end @implementation TableViewController - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *TableViewCellIdentifier = @"TableViewCellIdentifier"; TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier]; cell.imageView.image = [self.selectedIndexPathes containsObject:indexPath] ? [UIImage imageNamed:@"selected_image"] : [UIImage imageNamed:@"unselected_image"]; return cell; } #pragma mark - TableViewCellDelegate methods - (void)tableViewCell:(UITableViewCell *)cell didSelectImageView:(UIImageView *)imageView { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; if (![self.selectedIndexPathes containsObject:indexPath]) { [self.selectedIndexPathes addObject:indexPath]; } } @end @protocol TableViewCellDelegate <NSObject> - (void)tableViewCell:(UITableViewCell *)cell didSelectImageView:(UIImageView *)imageView; @end @interface TableViewCell : UITableViewCell @property (nonatomic, assign) id<TableViewCellDelegate> delegate; @end @interface TableViewCell() @property (nonatomic, strong) UIImageView *addToBasketImageView; @end @implementation TableViewCell - (void)awakeFromNib { [super awakeFromNib]; UITapGestureRecognizer *singleImageTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(addToBasketTapDetected:)]; singleImageTap.numberOfTapsRequired = 1; [self.addToBasketImageView addGestureRecognizer:singleImageTap]; } - (void)addToBasketTapDetected: (UIGestureRecognizer *)sender { [self.delegate tableViewCell:self didSelectImageView:self.addToBasketImageView]; } 

你明白我在这里做什么?