0

我已经实现了这个按钮抽屉,并添加了一些按钮。也就是说,我不确定如何从抽屉中的这些按钮向适当的委托方法发送消息,以从我的 tableView 中删除该项目。

如何获得正确的 indexPath?我应该为已切换检查按钮的 uitableviewcells 创建一个新的 NSIndex 吗?

谢谢

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSInteger directionMask = indexPath.row % 5;
    if (cell == nil) {
        cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];




    UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];

    drawerView.backgroundColor = [UIColor grayColor];
    cell.drawerView = drawerView;
        UIImage *checkImage = [UIImage imageNamed:@"check.png"];
        UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [checkButton setImage:checkImage forState:UIControlStateNormal];
        cell.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
        checkButton.frame = CGRectMake(10, 10, checkImage.size.width, checkImage.size.height);
        [drawerView addSubview:checkButton];

        [checkButton addTarget:nil action:@selector(onCheckMarkTap:) forControlEvents:UIControlEventTouchUpInside];

    }

- (void)onCheckMarkTap {
    NSLog(@"Delete the cell");

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        }

}
4

3 回答 3

1

这个问题已经有了答案。您可以使用带有事件的目标/动作或通过手势识别器获取在表格视图的坐标系中点击的按钮的坐标,然后使用 UITableView 的方法-indexPathForRowAtPoint:来获取包含该按钮的单元格的 indexPath。

于 2013-03-24T01:18:00.810 回答
0

Within cellForRowAtIndexPath:

checkButton.tag = indexPath.row

Also inside onCheckMarkTap, do this:

- (void)onCheckMarkTap : (id) sender
{
    if (sender.tag == 0)
    ///code
    else if (sender.tag == 1)
    ///code

}
于 2013-03-24T01:04:25.393 回答
-2

我在自己的代码中处理此类问题的方法是对“ UIButton”进行子类化,其中我将“ NSIndexPath”或“ tableRow”ivar 添加到子类化按钮中。让我们的名字是“ StangButton”。

使用子类按钮,您可以在通过“”组成单元格时填充“ indexPath” ivar cellForRowAtIndexPath:

然后,当按下按钮时,您将知道按下了哪个按钮以及它在哪一行。

您还可以检查"tableView:didSelectRowAtIndexPath:"在单元格中触摸按钮时是否调用了表格视图委托方法。

于 2013-03-24T01:04:42.023 回答