检测 UITableViewCell 中包含多个部分的 UITableView 中的按钮的点击 [英] Detect tap on a button in UITableViewCell for UITableView containing multiple sections

查看:27
本文介绍了检测 UITableViewCell 中包含多个部分的 UITableView 中的按钮的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to detect button tap on a UITableViewCell where the parent UITableView consists of multiple sections.

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

I was able to do it in the case of single section, but I got into trouble determining the correct section where there were more than one.

我正在尝试确定与用户点击按钮的 UITableViewCell 对应的正确部分.

I am trying to determine the correct section corresponding to the UITableViewCell where the user has tapped on the button.

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

Here is the updated code after the problem is being solved:

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

我要实现的布局如下图:

I want to achieve the layout as shown below:

此链接也为我的问题提供了解决方案:

This link gave the solution to my problem too:

推荐答案

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))
}

这篇关于检测 UITableViewCell 中包含多个部分的 UITableView 中的按钮的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆