区分表视图中的按钮和行单击事件

时间:2014-04-15 12:46:11

标签: ios objective-c uitableview uibutton

在我的UITableView应用程序中,我将UIButton放在每个单元格中。

但是当我点击按钮然后某些时候按钮事件将会到来并且有时候单击Cell的行

点击 点击事件按钮点击事件 >。

如何解决这个问题?

所有行都相同(意味着我没有使用可重复使用的)。

我的代码是......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *cellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell1"];

    [cell clearsContextBeforeDrawing];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(5,220,70, 30);
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
    [btn setTitle:[NSString stringWithFormat:@"button == %ld",(long)indexPath.row] forState:UIControlStateNormal];
    btn.tag = indexPath.row;
    [btn addTarget:self action:@selector(methodOfButton:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:btn];
}

return cell;

}

任何教程,代码,链接都会有很大帮助。

1 个答案:

答案 0 :(得分:0)

当用户轻敲按钮但不在按钮上时,很可能会出现问题。在其他界面元素中,您不会注意到这一点,因为不会生成其他事件,但在您的情况下,'didSelectRow'将会触发。

您需要确保按钮在单元格上占用尽可能多的空间(至少是完整的单元格高度)。通过这种方式,您可以减少用户错过点按的机会。现在你的按钮只有30px高,细胞的高度是多少?