2011-10-05 70 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通过

相关问题