2011-10-05 65 views

回答

4
// Customize the appearance of table view cells. 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    UITextField *textField = [[UitextField alloc] init]; 
... 
    textField.delegate = self; 
    textField.tag = indexPath.row; 
    [cell addSubView:textField]; 
} 
- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 
} 

我希望這可以幫助你(becomeFirstResponder)文本框...

0

如果你想文本字段,成爲第一個響應加載表視圖後,

NSIndexPath *indexPath = /* Index path of the cell containg the text field */; 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
// Assuming that you have set the text field's "tag" to 100 
UITextField *textField = (UITextField *)[cell viewWithTag:100]; 
// Make it the first responder 
[textField becomeFirstResponder]; 
0

的UITextField從UIResponder,它實現了becomeFirstResponder消息繼承。

1
// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

} 
if(indexPath.row==1) 
    { 
     [cell.txtField becomeFirstResponder]; 
    } 
return cell; 
} 

上面的代碼將所選排在第二的UITableView cell.Thanks

+0

搜索這種方式,您認爲 '細胞' 有一個屬性 'txtField'。爲此,您必須創建一個自定義單元格,否則將textField作爲子視圖添加到單元格中,然後調用becomeFirstResponder。如我錯了請糾正我。 – Mat

+0

@Mat,你說的沒錯。 –

0

斯威夫特4:爲了使文本框的桌面視圖中的第一響應者只需將此擴展名添加到tableView:

extension UITableView { 
    func becomeFirstResponderTextField() { 
     outer: for cell in visibleCells { 
      for view in cell.contentView.subviews { 
       if let textfield = view as? UITextField { 
        textfield.becomeFirstResponder() 
        break outer 
       } 
      } 
     } 
    } 
} 

然後在viewDidAppear()調用becomeFirstResponderTextField():

func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    tableView.becomeFirstResponderTextField() 
} 

注: 我認爲visibleCells通過

相關問題