{
NSIndexPath *_indexPath;
}
-(void)buttonClicked:(UIButton *)button{
UITableViewCell *cell = (UITableViewCell *)[button superview];//获取cell(通过button父视图获取)
_indexPath = [tableView indexPathForCell:cell];//这样就获取到indexPath了
}
//使用手势的时候同理也可以通过获取父视图来获取indexPath
-(void)TapGesture:(UITapGestureRecognizer *)tap{
UITableViewCell *cell = (UITableViewCell *)[[button superview]superview];
_indexPath = [tableView indexPathForCell:cell];//这样就获取到indexPath了
}
本文介绍了一种在SwiftUI中通过按钮点击或手势操作获取TableView中单元格indexPath的方法。具体实现方式为:从触发事件的按钮出发,找到其所在的父视图即单元格,再通过UITableView的indexPathForCell方法获得indexPath。

被折叠的 条评论
为什么被折叠?



