表格视图中的按钮没有响应其单元格编号

Posted

技术标签:

【中文标题】表格视图中的按钮没有响应其单元格编号【英文标题】:Button in table view not responding with its cell number 【发布时间】:2012-12-26 09:33:53 【问题描述】:

我在每个单元格中有一个表格视图,两个按钮作为编辑和取消。当我在同一单元格编号中同时单击编辑按钮时,取消按钮图像应该改变。例如。当用户选择第二个单元格编辑按钮时,只有第二个单元格中的取消按钮应该改变它的图像。

我的问题是当我点击任何单元格编辑按钮时,另一个单元格取消按钮图像会自动更改。

我希望当用户单击第一个单元格时,只有第一个单元格取消按钮应该更改而不是其他单元格?请给我样品我们怎么能做到这一点?据我所知,这种重复使用细胞的原因?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

     simpleTableIdentifier = @"MenuNameCell";
     MenuNameCell *cell = (MenuNameCell *)[tableView    dequeueReusableCellWithIdentifier:simpleTableIdentifier];

      if (cell== nil) 
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MenuNameCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        NSLog(@"---------new cell agin");   
    
       else
    
        NSArray *arrayView = [cell.contentView subviews];
        for (UIView *vTemp in arrayView)
        
            [vTemp removeFromSuperview];
        

        NSLog(@"---No New Cell hiiii");

    
    // Creating Label Menu Name
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
    _nameLabel.backgroundColor = [UIColor clearColor];
    _nameLabel.text =  [_hotel._orderedMenus objectAtIndex:indexPath.row];

    // Creating Label Menu Cost
    _amountMenu   = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
    _amountMenu.backgroundColor = [UIColor clearColor];
    _amountMenu.text  = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];

    // Creating Text Field For Order Quantity
    _textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
    _textFieldQuantity.userInteractionEnabled = NO;
    _textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];

    // Creating Button For Check Order
    _checkButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    [_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
    [_checkButton setTag:indexPath.row];
    [_checkButton setBackgroundImage:[UIImage imageNamed:@"edit.png"]forState:UIControlStateNormal];
    [_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];


    // Creating Button For CANCEL Order
    _cancelButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
    [_cancelButton setBackgroundImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
    [_cancelButton setTag:indexPath.row];
    [_cancelButton addTarget:self  action:@selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];

    [imageViewArray addObject:_checkButton];
    [imageViewArray addObject:_cancelButton];


    // Adding All To Content View
    [cell.contentView addSubview:_nameLabel];
    [cell.contentView addSubview:_amountMenu];
    [cell.contentView addSubview:_textFieldQuantity];
    [cell.contentView addSubview:_checkButton];
    [cell.contentView addSubview:_cancelButton];


    //objc_setAssociatedObject(_checkButton, iindex, indexPath,OBJC_ASSOCIATION_RETAIN );

    return cell;
    


      -(void)editQuantity:(id)sender


    button = (UIButton *)sender;
    row = button.tag;
    col = button.titleLabel.tag;
    _textFieldQuantity.userInteractionEnabled = YES;
    UIImage *buttonImage = [UIImage imageNamed:@"edit_over.png"];
    [_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    UIImage *buttonImageCancel = [UIImage imageNamed:@"check.png"];
    [_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
    _cancelButton.tag =  0;




     -(void)cancelOreder:(id)sender


    button = (UIButton *)sender;
    row = button.tag;
    NSLog(@"The Row Selected iS At Cancel Order ISSSS----%d", row);
    if (_cancelButton.tag ==  0)
        _textFieldQuantity.userInteractionEnabled = NO;
        UIImage *buttonImageCancel = [UIImage imageNamed:@"check_over.png"];
        [_cancelButton setBackgroundImage:buttonImageCancel  forState:UIControlStateNormal];
        UIImage *buttonImageCancel1 = [UIImage imageNamed:@"cancel.png"];
        [_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
        UIImage *buttonImage = [UIImage imageNamed:@"edit.png"];
        [_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
        _cancelButton.tag = 1;
    
    else
    
        UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:@"iHomeDelivery" message:@"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    



     -(void)removeEntry:(id) sender
    _hotel = [HotelList sharedInstance];
    NSLog(@"Called...%d",[_hotel._orderedMenus count]);
    NSLog(@"Button index is ------%d", row);
    if ([_hotel._orderedMenus count] > 1)  // if table contains multiple menus
        [_hotel._orderedMenus removeObjectAtIndex:row];
        [_hotel._menuPrices removeObjectAtIndex:row];
        [_hotel._selectedQuantity removeObjectAtIndex:row];
        NSNumber *aNumber;
        int sum = 0;
        for (aNumber in _hotel._menuPrices)
        
            int aInt = [aNumber intValue];
            sum = sum + aInt;
        
        _labelFinalBillAmount.text = [NSString stringWithFormat:@"%d", sum];
    
    else if ([_hotel._orderedMenus count] ==  1) // if table contain only single menu // Here All array gets Empty
        [_hotel._orderedMenus removeAllObjects];
        _labelFinalBillAmount.text = @"0";
    
    NSLog(@"--count is %d",[_hotel._orderedMenus count]);
    [_tableviewMenus reloadData];

【问题讨论】:

Have a look at this post 自定义UITableViewCell,它将解决您的许多问题,包括重复使用单元格、更改背景等。 【参考方案1】:

您正在使用自定义单元格,然后将此代码添加到您的自定义类中并获取特定单元格的特定发件人

-(IBAction)yourButtonMehod:(id)sender

    UIView *senderButton = (UIView*) sender;
    NSIndexPath *indexPath = [(UITableView *)self.superView indexPathForCell: (UITableViewCell*)[[senderButton superView]superView]];

现在,您可以获取发件人并使用 indexpath.row 进行任何操作

【讨论】:

【参考方案2】:

在您的按钮操作方法中

-(IBAction)cellButtonPressed:(id)sender withEvent:(UIEvent *)event

    UITouch * touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: bulletinTable];
    NSIndexPath * indexPath = [bulletinTable indexPathForRowAtPoint: location];

还有你的 cellForRowAtIndexPath 方法

[cell.accessory addTarget:self action:@selector(cellButtonPressed:withEvent:) forControlEvents:UIControlEventTouchUpInside];

这应该可行。

【讨论】:

我如何使用 IndexPath 位置按钮更改按钮图像 我不明白您在评论中提到的问题?请提供详细信息 如果用户单击同一单元格上的编辑按钮,我想更改取消按钮图像。对于前;如果用户点击第三个单元格编辑按钮,那么同时第三个单元格取消按钮会被触发,但它没有发生

以上是关于表格视图中的按钮没有响应其单元格编号的主要内容,如果未能解决你的问题,请参考以下文章

是否在表格视图单元格上选择没有响应

故事板中的自定义单元格行高度设置没有响应

如何让 UITableView 标题视图出现在响应者链中的表格单元格之前?

将 UITextView 作为第一响应者后,表格视图单元格不可选择

在 ios 的滚动视图内检测按钮的触摸

UITableView 单元格在调整大小后不响应输入