我有一個按鈕是自定義UITableViewCell的一部分。我想要的是當有人點擊按鈕時,爲該單元調用適當的tableView:cellForRowAtIndexPath。如何在表格視圖中調用正確的tableView:cellForRowAtIndexPath?
如何將按鈕綁定到正確的cellForRowAtIndexPath?
任何建議和/或代碼示例將不勝感激
我有一個按鈕是自定義UITableViewCell的一部分。我想要的是當有人點擊按鈕時,爲該單元調用適當的tableView:cellForRowAtIndexPath。如何在表格視圖中調用正確的tableView:cellForRowAtIndexPath?
如何將按鈕綁定到正確的cellForRowAtIndexPath?
任何建議和/或代碼示例將不勝感激
您可以從touch事件中獲取indexPath。修改您的buttonTapped方法是這樣的:
- (void) buttonTapped:(id) sender forEvent:(UIEvent *)event
,然後使用類似以下內容:
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:tableView]];
如果只有一個部分,你也許可以使用UIView
的tag
財產。
類似以下的東西理論上會起作用。
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)idxPath
{
Your_UITableViewCell_Subclass * cell = [tableView dequeueCellWithIdentifier:@"..."];
cell.button.tag = idxPath.row;
....
return cell;
}
....
- (void) buttonTapped:(id) sender
{
int idx = ((UIButton *)sender).tag;
NSIndexPath * idxPath = [NSIndexPath indexPathForRow:idx inSection:0];
UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:idxPath];
// Do whatever you want to do with the cell
}
此解決方案將無法處理多個節。 – danielbeard