2013-01-23 70 views
0
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"searchCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    searchField = (UITextField *)[cell viewWithTag:10000]; 

    [searchField resignFirstResponder]; 
} 

好的,我有一個標識符名稱爲「searchCell」的單元。此單元格包含一個帶標記10000的單個UITextField,它在屏幕底部彈出一個鍵盤。當用戶觸摸另一個單元格時,必須隱藏鍵盤,以便用戶可以有更大的空間來上下滾動。iOS)在UITableView中的單元格中檢測鍵盤

但是,當一個鍵盤彈出並且用戶觸摸(選擇)一個單元格時,上面的代碼被調用但不工作...... :(看起來好像分配的UITableViewCell不是用戶當前的那個用。我在做什麼錯在這裏?

+0

添加'forIndexPath'根本沒有幫助任何btw。 – Raccoon

+0

你有代表設置在你的筆尖和你的.h文件中嗎?還要這樣做=> UITextField * txtFieldObj =(UITextField *)[cell viewWithTag:10000];在didselectRowAtIndexpath中做到這一點,添加一個if(yourIndexpath.row == 0),然後只是退出鍵盤或如果(yourindexpath!= 0),然後辭職別人做別的事情.. [txtFieldObj resignFirstResponder];還有NSLog(@「txtField%@」,textFieldObj); //檢查它是否爲空... –

+0

你設置了你的文本框的代表嗎?如果不嘗試'textfield searchfield.delegate = self;'並調用'[searchField resignFirstResponder];' - (BOOL)textFieldShouldReturn:(UITextField *)textField'方法 –

回答

2
  1. 使類的UITextField
  2. 轉至故事板文件的委託,點擊文本字段,並轉到連接檢查
  3. 在網點,連接代理到視圖控制器
  4. 在模擬器中運行它。 t會工作
+0

工作!謝謝 !!! – Raccoon

+0

完全不知道我必須將代理連接到View Controller。 – Raccoon

相關問題