ios - 使用dequeueReusableCellWithIdentifier ios将单元格中的ImageView绑定(bind)到错误的行号

标签 ios uitableview

我有 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 - 是我可以将图像绑定(bind)到单元格的唯一位置,但它与行/节号一起播放错误。如何处理?

最佳答案

addToBasketTapDetected 方法中,您没有向我们展示您实际上是如何找到 theTappedImageView 的,我怀疑这就是问题所在。

但无论如何,我根本不会在 TableView Controller 中执行此操作,而是将 UIViewCell 子类化并在其中处理点击,因为您可以确定您拥有正确的单元格。

此外,我不会使用 UIImageView,而是使用 UIButton,您可以在其中配置两种状态:UIControlStateNormalUIControlSelected,当按钮被点击时,将其设置为 button.selected = YES(如果再次点击则设置为 NO)。这将为您提供一个切换按钮。

关于ios - 使用dequeueReusableCellWithIdentifier ios将单元格中的ImageView绑定(bind)到错误的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161104/

相关文章:

ios - 在 iPhone 应用程序和 Arduino 服务器之间安全传输数据的方法

ios - 如何在 Swift 中创建一组委托(delegate)协议(protocol)项?

ios - 自定义表格 View 单元格未根据 Nib 调整大小

ios - 图像不是第一次加载到 iOS 10 的 tableview 中,而是滚动后?

iphone - UILabel 改变位置

iphone - 更改UIButton文本的颜色和对齐方式

iOS 背景通知栏

ios - 如何在 Swift 中将本地时间日期转换为 UTC?

iphone - UITableViewController 不显示工具栏项目?

ios - 将披露指示器添加到表格 View 单元格