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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测点击UITableViewCell中包含多个部分的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))
}
另一答案
Swift 3 

let clickedButtonIndexPath = self.tableView.indexPathForRow(at: touchPoint)
        if(clickedButtonIndexPath != nil)
        {
            NSLog("index path.section ==%ld", Int(clickedButtonIndexPath!.section))
            NSLog("index path.row ==%ld", Int(clickedButtonIndexPath!.row))
           }
另一答案

在不使用CGPoint并对UI进行假设的情况下获得索引路径的更直接方法是查看.superview。我想这在实践中更可靠。

- (myTableViewCell *)cellContainingView:(UIView *)view
{
    do {
        view = view.superview;
    } while (view != nil && ![view isKindOfClass:[myTableViewCell class]]);

    return (myTableViewCell *)view;
}

然后您可以在按钮操作中执行此操作:

- (IBAction)myButtonPressed:(id)sender
{
    myTableViewCell *cell = [self cellContainingView:(UIView *)sender];
    NSIndexPath *path = [self.tableView indexPathForCell:cell];
}

以上是关于检测点击UITableViewCell中包含多个部分的UITableView的按钮的主要内容,如果未能解决你的问题,请参考以下文章

检测数组(网页中包含多个框架时)

将“标签状态栏滚动到顶部”添加到 UIScrollView 中包含的 UITableView

在 UITableViewCell 中包含图像的 CollectionView

iOS 中包含 UIWebView 的单元格的动态 UITableViewCell 高度

如何区分用户点击我的表格视图单元格和用户点击单元格中包含的子视图?

Flutter:我们检测到您的应用在您的 1 个或多个 app bundle 或 APK 的清单文件中包含 requestLegacyExternalStorage 标志