2016-05-26 28 views
0

我使用下面的代碼來檢測對UITableView的點擊,並根據單擊哪個單元格和單元格中的哪個元素進行操作,並對任何不匹配的元素執行默認操作。如何在表格末尾添加indexPath爲空格的第一個單元格的indexPath?

-(void)addTapRecognizer { 
    // this is called when the view is created 
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
    singleTap.delegate = self; 
    singleTap.numberOfTapsRequired = 1; 
    singleTap.numberOfTouchesRequired = 1; 
    [self.tableView addGestureRecognizer:singleTap]; 
} 

- (void)handleTap:(UITapGestureRecognizer *)tap { 
    NSLog(@"tap detected!"); 
    if (UIGestureRecognizerStateEnded != tap.state) { 
     return; 
    } 
    UITableView *tableView = (UITableView *)tap.view; 
    CGPoint p = [tap locationInView:tap.view]; 
    NSIndexPath* indexPath = [tableView indexPathForRowAtPoint:p]; 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    NSLog(@"selectedIndex = %ld", (long)indexPath.row); 
    // take action depending where the cell was clicked 
    // with a default action if no element matches 
    MyTableViewCell *cell = (MyTableViewCell *) [self.tableView cellForRowAtIndexPath:indexPath]; 
    CGPoint pointInCell = [tap locationInView:cell]; 
    if(CGRectContainsPoint(cell.someImage.frame,pointInCell)) { 
     [self openItemID:[ItemList[indexPath.row] valueForKey:ID_ITEM]]; 
     return; 
    } 
    if (...) { 
     ... 
     return; 
    } 
    [self openItemID:[ItemList[indexPath.row] valueForKey:ID_ITEM]]; 
    return; 
} 

我的問題是,當沒有足夠的細胞,以填補屏幕(所以例如表包含2個細胞,然後在下面空格),當最後一個單元格下方的用戶點擊,這是處理作爲第一個單元格的點擊(控制檯在兩種情況下都會記錄「selectedIndex = 0」)。

有沒有辦法區分這種點擊在表格末尾的空白處和單擊表格的「正確」單元格之間的區別?

+1

我想知道在這種情況下'indexPath'' nil'。我不會奇怪,'nilIndexPath.row'值爲0. – Larme

+0

Ooh ... spot on,我不認爲nil.row會給0,indexPath是零而不是。謝謝!雖然我仍然對Gordonium的答案感興趣,這似乎是一個更清晰的方法來做到這一點 – user14764

+1

顯然,只需要檢測'UITableViewCell'上的選擇必須使用'tableView:didSelectRowAtIndexPath:'完成。但有些人有時想在他們身上添加多個手勢,所以你的問題似乎是合法的。 – Larme

回答

1

有沒有辦法區分這種點擊在表格末尾的空白處和點擊表格的「正確」單元格之間的區別?

是的。對於廉價和簡單的解決方案只能做你正在嘗試,如果你真正得到的indexPath做:

NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:p]; 
if(indexPath != nil){ 
    // do everything in here 
} 

基本上,你indexPath將返回零,因爲它無法找到一行。 From the docs

表示與點相關聯的行和節的索引路徑,如果該點超出任何行的範圍,則爲nil。

你可以做到這一點,你現在這樣做,但沒有任何理由,你爲什麼不使用方式:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

這是檢測細胞更加標準的方式,用戶點擊。

如果您點擊某個不是單元格並具有其他許多優點的方法,則不會調用此方法。首先你可以免費獲得對tableView和indexPath的引用。但是你也不需要這樣的手勢識別器。

嘗試這樣:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    // Do stuff with cell here... 
} 

顯然,這一切都假定你已經正確設置了類作爲您的表視圖的委託。

注意:當使用Xcode的自動完成功能來執行此操作時,很容易錯誤地編寫didDeselectRowAtIndexPath而不是didSelectRowAtIndexPath。我總是這樣做,然後在20分鐘後不可避免地意識到我的錯誤。

+0

我正在使用它以便能夠檢測單元的哪個元素被點擊。我沒有找到一種方法來通過 - (無效)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,我正在開發的應用程序(最初不是我開發的)使用這種手勢識別器方法非常多。 – user14764

+0

啊。相反,您希望從您的單元中委託回調。基本上將您的表視圖類設置爲自定義單元類的代理。然後,在自定義單元類中,每當點擊一個按鈕(或類似元素)時,調用單元的委託(這是您的表視圖類),然後在那裏處理該調用。 – Gordonium

+0

謝謝。如Larme的評論中所建議的,檢查indexPath是否爲零。來自單元格的代表回調看起來更清晰,但如果我理解得很好,它需要一個自定義表視圖類...所以我現在可能會堅持使用手勢識別器的方式;) – user14764

相關問題