简体   繁体   中英

Differentiate button and row click event in Table View

In my Application in UITableView i put UIButton in every cell.

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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