使用dequeueReusableCellWithIdentifier ios

时间:2016-05-11 11:20:50

标签: 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 - 是我可以将图像绑定到单元格的单个位置,但它与行/节号相关。如何处理?

2 个答案:

答案 0 :(得分:1)

在方法addToBasketTapDetected中,您没有向我们展示您实际找到theTappedImageView的方式,我怀疑这是出了什么问题。

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

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

答案 1 :(得分:1)

@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];
}

你明白我在这做什么吗?