【问题标题】:Detect tap on a button in UITableViewCell for UITableView containing multiple sections检测对包含多个部分的 UITableView 的 UITableViewCell 中的按钮的点击
【发布时间】:2015-10-17 09:50:30
【问题描述】:

我想检测 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];
    }
}

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

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

【问题讨论】:

    标签: ios objective-c iphone swift uitableview


    【解决方案1】:

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

    【讨论】:

    • 我遇到了问题>在某些情况下有任何想法,兄弟
    • 这发生在随机场景中,你面临的场景
    • 你可以检查这个问题stackoverflow.com/questions/31692692/…
    • 有效的 Swift 代码是 let touchPoint: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView) let buttonIndexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(touchPoint)!
    • Swift 3.1 - var clickedButtonIndexPath = mainTable.indexPathForRow(at: touchPoint)
    【解决方案2】:
    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】:

      斯威夫特 5

       let touchPoint: CGPoint = sender.convert(.zero, to: tableViews)
          let clickedButtonIndexPath = self.tableViews.indexPathForRow(at: touchPoint)
                  if(clickedButtonIndexPath != nil)
                  {
                      NSLog("index path.section ==%ld", Int(clickedButtonIndexPath!.section))
                      NSLog("index path.row ==%ld", Int(clickedButtonIndexPath!.row))
                     }
      

      【讨论】:

        【解决方案4】:

        在不使用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];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-30
          • 2019-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多