![](/img/trans.png)
[英]How to get the indexPath.row when I click a button on a table view cell?
[英]Differentiate button and row click event in Table View
在我的UITableView應用程序中,我將UIButton放在每個單元格中。
但是,當我單擊“按鈕”時, 有時會發生按鈕事件,而有時會單擊“單元格的行” 。
我需要按鈕單擊事件一切的時候,按一下按鈕和行單擊事件時行點擊 。
如何解決這個問題?
所有的行都是相同的(意味着我沒有使用可重用的)。
我的代碼是...
- (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;
}
任何教程,代碼,鏈接都會有很大的幫助。
您看到的問題最有可能是在用戶輕按按鈕而不是在按鈕上時發生的。 在其他界面元素中,您不會注意到這一點,因為不會生成其他事件,但是在您的情況下會觸發“ didSelectRow”。
您需要確保按鈕在單元格上占據盡可能多的空間(至少在整個單元格高度上)。 這樣,您將獲得較少的用戶錯過點擊的機會。 現在您的按鈕只有30px高,單元格的高度是多少?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.