简体   繁体   English

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

[英]Differentiate button and row click event in Table View

In my Application in UITableView i put UIButton in every cell. 在我的UITableView应用程序中,我将UIButton放在每个单元格中。

but when i click on Button then some times button event will come and some time Cell's Row clicked . 但是,当我单击“按钮”时, 有时会发生按钮事件,而有时会单击“单元格的行”

I need to get button click event all the time when button click and row click event when row clicked . 我需要按钮单击事件一切的时候,按一下按钮行单击事件行点击

How to come over this issue ? 如何解决这个问题?

All the Row are identical (means i didn't use reusable). 所有的行都是相同的(意味着我没有使用可重用的)。

My code for this is... 我的代码是...

- (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;

}

Any tutorial, code, link will be great help. 任何教程,代码,链接都会有很大的帮助。

Most likely the problem you're seeing happen when user tapped close to the button but not on it. 您看到的问题最有可能是在用户轻按按钮而不是在按钮上时发生的。 In other interface elements you will not notice this, since no other event will be generated but in your cases 'didSelectRow' will fire. 在其他界面元素中,您不会注意到这一点,因为不会生成其他事件,但是在您的情况下会触发“ didSelectRow”。

You need to make sure button occupies as much room as possible on the cell (at least full cell height). 您需要确保按钮在单元格上占据尽可能多的空间(至少在整个单元格高度上)。 This way you will get user less chance to miss tap. 这样,您将获得较少的用户错过点击的机会。 Right now your button is only 30px tall, what the height of the cell? 现在您的按钮只有30px高,单元格的高度是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM