我有一個tableview,其中有兩個動作,第一個是我使用委託didSelectRowAtIndexPath,第二個我想在我的單元上的按鈕上使用動作來做其他事情。我的問題是,我沒有成功獲得indexpath?最佳做法是什麼?如何從單元格中的按鈕操作獲取tableview中的indexPath?
回答
如果已經添加的按鈕對小區的,
[cell.contentView addSubview:button];
那麼,就可以得到該指數路徑,
- (void)onButtonTapped:(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// Go ahead
}
謝謝! UIButton * button =(UIButton *)sender; UITableViewCell * cell =(UITableViewCell *)button.superview.superview; –
好的!歡迎! – EmptyStack
您不應該從超級視圖訪問tableView。雖然這現在可行,但它可能不適用於新的iOS。來自6/7 Apple的經典示例增加了另一個超級視圖。這會崩潰 – CW0007007
這裏是一個自定義按鈕的完整代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCellFilter *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {cell = [[CustomCellFilter alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
//configure your cell here
cell.titleLabel.text = @"some title";
//add button
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(40, 10, 18, 18);
[resetButton addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[resetButton setImage:[UIImage imageNamed:@"someimage.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:myButton];
//afterwards return the cell
return cell;
- (void)onButtonTapped:(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [filterTable indexPathForCell:cell];
NSLog(@"%@", indexPath);
//use indexPath here...
}
如果您的按鈕被移動或者Apple更改了附件視圖的視圖層次結構,該方法會沿着鏈條向上看對於一個UITableViewCell:
- (void)tapAccessoryButton:(UIButton *)sender {
UIView *parentView = sender.superview;
// the loop should take care of any changes in the view heirarchy, whether from
// changes we make or apple makes.
while (![parentView.class isSubclassOfClass:UITableViewCell.class])
parentView = parentView.superview;
if ([parentView.class isSubclassOfClass:UITableViewCell.class]) {
UITableViewCell *cell = (UITableViewCell *) parentView;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
我使用這個解決方案 - 讓使用細胞分
指數路徑CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);
它的工作完美
這一個更合適,而不是使用'superView.superView' – Mrug
如果您使用的是集合視圖,你可以使用約傑什的方法,但改變indexPathForRowAtPoint爲indexPathForItemAtPoint這樣的:
CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);
- 1. 如何從單元格獲取UITableViewCell indexPath?
- 2. 如何知道在UITableView中按鈕點擊Tableview單元格的indexPath/row?
- 3. tableview中的單選按鈕單元格
- 4. 如何通過單擊按鈕來獲取tableview單元格中textfield的值?
- 5. 將indexpath標記傳遞給tableview中的原型單元格內的按鈕
- 6. 如何從按鈕按下事件(自定義tableview單元格)中獲取單元格數據
- 7. 如何在uitextfield中獲取單元格的indexpath委託方法?
- 8. 在tableview單元格中標記按鈕
- 9. 獲取tableview行的indexpath
- 10. 如何在特定的indexpath的tableview中的單元格中插入值?
- 11. 通過數據取決於tableView中的按鈕單元格
- 12. 如何在單擊自定義附件按鈕時獲取UITableView中單元格的Indexpath?
- 13. 如何獲取DataGrid中的單元格值按鈕C#
- 14. 如何查找調用[tableView setEditing:NO]時選擇的單元格indexPath?
- 15. 如何獲取容器UITableView中的TableView的單元格
- 16. 從tableView獲取indexPath.row到單元格
- 17. 如何根據單元格文本獲取UICollectionViewCell的indexPath?
- 18. 如何從tableview中檢查單元格?
- 19. 如何獲取tableview單元格中的值
- 20. 如何從qtablewidget中檢測單元格中單擊的按鈕
- 21. 如何在tableView中獲取表格目標單元格:heightForRowAtIndexPath:?
- 22. 從tableview單元格中提取數據?
- 23. 如何通過點擊單元格上的按鈕從我的TableView中刪除單元格?
- 24. 無法從自定義tableview單元格按鈕中刪除行
- 25. 如何在Tableview中獲取指定行的IndexPath?
- 26. 如何讓表格單元格按鈕進行操作
- 27. 按鈕位於單元格中時獲取按鈕位置
- 28. 當按下TableView單元格中的按鈕時,我怎麼知道按下了哪一行單元格按鈕?
- 29. 使用[tableView dequeueReusableCellWithIdentifier:forIndexPath:]將單元格中的按鈕添加到按鈕中。
- 30. 如何使用Objective C獲取子單元的IndexPath第一節tableview?
可能DUPLIC吃了[IOS 7 - 如何從放置在UITableViewCell中的按鈕獲取indexPath](http://stackoverflow.com/questions/19000356/ios-7-how-to-get-the-indexpath-from-button-placed- in-uitableviewcell) –