ios - 检测点击包含多个部分的 UITableView 的 UITableViewCell 中的按钮

标签 ios objective-c iphone swift uitableview

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

我能够在单个部分的情况下做到这一点,但我在确定多个部分的正确部分时遇到了麻烦。

我正在尝试确定与用户点击按钮的 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];
    }
}

我想实现如下图的布局:

enter image description here

这个链接也解决了我的问题:

最佳答案

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/

相关文章:

ios - Swift:UIViewController 不会自动取消初始化

ios - 从 UIPickerView 更改按钮图像

objective-c - xcode - 在框架和工作区之间共享代码

iphone - 录音音质不好

iphone - NSMutableString appendFormat ... 添加空格?

ios - 尝试使用 UrbanAirship 创建推送通知时,如何指定登陆哪个屏幕?

ios - Facebook 页面插件不在 Safari 中滚动?

iphone - 我们可以将图像缩放到设备的全尺寸作为 iOS 中的媒体播放器吗?

iphone - 如何以安全的方式为其他开发人员构建框架或库?

ios - 如何在导航栏上添加自定义 View