我有表單元格;每個單元包含帶有電話號碼的標籤。當我點擊標籤時,出現UIMenuController
。按標籤查找單元格的索引路徑
是否有一種主要的方法來查找包含選定標籤的單元格的indexPath? (didSelectRowAtIndexPath:
不應該叫)
我可以想象很多骯髒的黑客,但我尋找一個主要/好的解決方案。
UPD
我有參考選擇的標籤。
我有表單元格;每個單元包含帶有電話號碼的標籤。當我點擊標籤時,出現UIMenuController
。按標籤查找單元格的索引路徑
是否有一種主要的方法來查找包含選定標籤的單元格的indexPath? (didSelectRowAtIndexPath:
不應該叫)
我可以想象很多骯髒的黑客,但我尋找一個主要/好的解決方案。
UPD
我有參考選擇的標籤。
編輯:
這是一個更好的方法。
- (IBAction)someMethod:(id)sender {
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
}
很容易!請告訴我,爲什麼.superview打了兩次電話? – 2014-11-06 21:54:16
contentView在iOS 7中存在tableViewCells – 2014-11-06 21:55:50
在包含發件人和完成的IBAction方法中執行編輯後的答案。更乾淨。 – 2014-11-06 21:58:03
你可以去:
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
{
[cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}
for (UITableViewCell *cell in cells){
for (UILabel *label in cell.subviews){
if (label == yourLabel)
indexPath = [tableView indexPathForCell: cell];
}
}
我知道這個算法,但我應該掃描整個表,那嚇唬我(因爲O(n))。 – 2014-11-06 21:56:35
您可以使用UITableView方法 indexPathsForVisibleRows避免O(n)問題,然後檢查該列表,但使用UITableView方法indexPathForRowAtPoint:,如DCGoD的答案中所述,更清晰和更簡單。 – 2014-11-06 23:42:53
從您的標籤,搜索在標籤的層次結構中的單元對象:
-(UITableViewCell*) cellContainingView:(UIView*)view
{
UIView* currentView = view;
while (currentView)
{
if ([currentView isKindOfClass:[UITableViewCell class]])
{
return (UITableViewCell*)currentView;
}
currentView = currentView.superview;
}
return nil;
}
一旦你的細胞,稱之爲[tableView indexPathForCell:cell]
使用而不是硬編碼view.superview.superview使你的代碼更強大,因爲它通過系統的版本來處理單元視圖層次結構中可能發生的變化。
使用UITableView方法'indexPathForRowAtPoint:',如DCGoD的答案中所述,更清潔和更簡單。 – 2014-11-06 23:42:18
嗨,爲什麼不應該調用SelectRowAtIndexPath? – james075 2014-11-06 21:49:33
對於拳頭,我不會選擇行時觸摸標籤,第二,在這種方法中,我可以只存儲indexPath,作爲參數。 @ James03 – 2014-11-06 21:52:46