![](/img/trans.png)
[英]How do I maintain the selected index of UISegmentedControl inside a UITableViewCell when the cell is reloaded?
[英]Button inside UITableViewCell - how do i find out what cell it's inside?
我的UITableView的每個單元格中都有一個按鈕,但是當按鈕被觸發時,我想知道它位於哪個單元格中。 我可以使用標記,也可以將按鈕子類化並為其添加indexPath,但是當我刪除或添加單元格時,問題就來了,我必須跟上所有這些按鈕的更新。 人們還能想到另一種方法嗎?
使用UITableView
的indexPathForCell:
方法。 它需要一個單元格,並為您提供該單元格的索引路徑。
UITableViewCell * cell = (UITableViewCell*) button.superview;
NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
嘗試這個
- (void) buttonAction:(id)sender{
UIButton *buttonInCell = (UIButton *)sender;
NSIndexPath *indexPathOfCell = [self.yourTable indexPathForCell:
(UITableViewCell *)[[buttonInCell superview] superview]];
}
其中buttonInCell保存發送方Button,indexPathOfCell保存具有特定按鈕的單元格的indexPath值,從該按鈕觸發了操作
以上方法可能不適用於iOS 7
更新
- (void)buttonAction:(id)sender {
UIButton *buttonInCell = (UIButton *)sender;
CGPoint center= buttonInCell.center;
CGPoint rootViewPoint = [buttonInCell.superview convertPoint:center toView:self.yourTableView];
NSIndexPath *indexPath = [self.yourTableView indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.