我想检测父UITableView由多个部分组成的UITableViewCell上的按钮点击。

在单节的情况下,我能够做到这一点,但是在确定一个以上的正确节时遇到了麻烦。

我试图确定与用户在按钮上点击的UITableViewCell对应的正确部分。

解决问题后,下面是更新的代码:

@property (strong, nonatomic) NSMutableArray* quantityArray;
@property (strong, nonatomic) NSMutableArray* rows;

@synthesize quantityArray,rows;

- (void)viewDidLoad {
    [super viewDidLoad];

    quantityArray = [[NSMutableArray alloc] init];
    [self PluMinusFunction];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //
        // Some code here.
        //

    if (addBtnClicked || minusBtnClicked) {
        cell.lblCount.text = [[quantityArray objectAtIndex:indexPath.section-1]objectAtIndex:indexPath.row];
        addBtnClicked = NO;
        minusBtnClicked = NO;
    } else {
        cell.lblCount.text = [[quantityArray objectAtIndex:indexPath.section-1]objectAtIndex:indexPath.row];
    }

        cell.btnMinus.tag = indexPath.row;
        cell.btnPlus.tag = indexPath.row;
        cell.lblCount.tag = indexPath.row;

        [cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];

        if ([ cell.lblCount.text integerValue]!=0) {
            [cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
        }

        return cell;
    }
}

#pragma mark - Plus Minus Button

- (void)PluMinusFunction {
    for (int j=0; j <[itemArr count]; j++) {
        rows = [[NSMutableArray alloc] init];
        for (int i=0; i <[ [[itemArr objectAtIndex:j] objectForKey:@"itemList"]count]; i++) {
            [rows insertObject:@"0" atIndex:i];
        }
        [quantityArray insertObject:rows atIndex:j];
    }
    [self sum];
}

#pragma mark - UIButton selector

- (void)addItem:(UIButton*)button {
    CGPoint touchPoint = [button convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];

    NSInteger row = clickedButtonIndexPath.row;
    NSInteger section = clickedButtonIndexPath.section;

    NSInteger  LabelText =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue] + 1;
    NSMutableArray *subArray = [quantityArray objectAtIndex:section-1];
    [subArray replaceObjectAtIndex:row withObject: [NSString stringWithFormat:@"%ld",(long)LabelText]];
    [quantityArray replaceObjectAtIndex:section-1 withObject: subArray];
    addBtnClicked = YES;

    NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:section];
    NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
    [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}

- (void)deleteItem:(UIButton*)button {
    CGPoint touchPoint = [button convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];

    NSInteger row = clickedButtonIndexPath.row;
    NSInteger section = clickedButtonIndexPath.section;

    NSInteger  LabelValue =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue];

    if (LabelValue >= 1) {
        NSInteger  LabelText =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue] - 1;
        NSMutableArray *subArray = [quantityArray objectAtIndex:section-1];
        [subArray replaceObjectAtIndex:row withObject: [NSString stringWithFormat:@"%ld",(long)LabelText]];

        [quantityArray replaceObjectAtIndex:section-1 withObject: subArray];
        addBtnClicked = YES;

        NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:section];
        NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
        [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
    }
}

我要实现如下所示的布局:

ios - 检测包含多个节的UITableView的UITableViewCell中的按钮上的点击-LMLPHP

该链接也为我的问题提供了解决方案:
  • Detecting which UIButton was pressed in a UITableView
  • 最佳答案

    Objective-C

    -(void)addItem:(UIButton*) sender
    {
    
    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:mainTable]; // maintable --> replace your tableview name
    NSIndexPath *clickedButtonIndexPath = [mainTable indexPathForRowAtPoint:touchPoint];
    
     NSLog(@"index path.section ==%ld",(long)clickedButtonIndexPath.section);
    
     NSLog(@"index path.row ==%ld",(long)clickedButtonIndexPath.row);
    
    
    }
    

    Swift3
     func addItem(sender: UIButton)
    {
        var touchPoint = sender.convert(CGPoint.zero, to: self.maintable)
        // maintable --> replace your tableview name
        var clickedButtonIndexPath = maintable.indexPathForRow(at: touchPoint)
        NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
        NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
    
    
    }
    

    Swift2及更高版本
    func addItem(sender: UIButton)
     {
    var touchPoint: CGPoint = sender.convertPoint(CGPointZero, toView: mainTable)
        // maintable --> replace your tableview name
    var clickedButtonIndexPath: NSIndexPath = mainTable(forRowAtPoint: touchPoint)
    NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
    NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
    }
    

    关于ios - 检测包含多个节的UITableView的UITableViewCell中的按钮上的点击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31649220/

    10-09 02:33