检测按钮单击表视图ios xcode多行和节

时间:2022-11-03 09:48:45

I want to detect the Button click on table view with multiple row and section. I was able to acheive it on case of single row but I got trouble that it didnt detect correct section. I got trouble to detect which section button has user clicked

我想检测具有多个行和节的表视图上的按钮单击。我能够在单排的情况下实现它,但我遇到麻烦,它没有检测到正确的部分。我无法检测用户点击了哪个部分按钮

Below is the updated code after 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 {
    ====================================================

        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 - Pluys 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 acheive as image below

我希望如下图所示

检测按钮单击表视图ios xcode多行和节

below link give solution to my problem too

以下链接也解决了我的问题

Detecting which UIButton was pressed in a UITableView

检测在UITableView中按下了哪个UIButton

3 个解决方案

#1


40  

Objective-C

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

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 and above

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

#2


1  

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

#3


0  

A more direct way to get the index path without using CGPoint and making assumptions about UI is to look at the .superview. I'd imagine this is more reliable in practice.

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

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

    return (myTableViewCell *)view;
}

Then you can simply do this in the button action:

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

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

#1


40  

Objective-C

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

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 and above

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

#2


1  

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

#3


0  

A more direct way to get the index path without using CGPoint and making assumptions about UI is to look at the .superview. I'd imagine this is more reliable in practice.

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

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

    return (myTableViewCell *)view;
}

Then you can simply do this in the button action:

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

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