簡體   English   中英

UISearchBar手勢識別器立即在tableview中選擇單元格

[英]UISearchBar gesture recognizer immediately selects cell in tableview

我的項目中有一個搜索欄和一個表格視圖設置。 現在,我想提供一種僅在用戶觸摸搜索欄外部任何地方時關閉鍵盤的好方法。

//Search bar tap recognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];
[tap setCancelsTouchesInView:NO];
[self.tableView addGestureRecognizer:tap];

到目前為止,該方法仍然有效-如果我將以下行省略掉:

[tap setCancelsTouchesInView:NO];

拍子識別器與didSelectRowAtIndexPath方法重疊,因此,如果我想選擇一行,則必須在單元格上滑動一下才能觸發didSelectRowAtIndexPath方法。

無論如何,如果您在搜索欄之外點擊該行,鍵盤就會消失,但是didSelectRowAtIndexPath也將被觸發-因為大多數情況下,用戶只會在屏幕中間點擊-在表格視圖的某個單元格處。

如果搜索欄仍處於“活動”狀態,如何避免第一次調用表視圖方法didSelectRowAtIndexPath 因為我認為這很煩人,如果您只想關閉鍵盤,然后您會看到所點擊的單元格的詳細信息視圖。

為了僅在用戶觸摸搜索欄以外的任何地方時都關閉鍵盤,可以在表視圖didSelectRowAtIndexPath委托內調用“關閉鍵盤”方法。 因此它將根據需要工作。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [self dismissKeyboard];
    // you can write detail page navigation code here.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if([self.view isFirstResponder])
{ 
[self.view endEditing:YES];
cell.userInteractionEnabled = NO;
} else {
cell.userInteractionEnabled = YES;
    //if you have to do some actions when cell is selected
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM