0

像许多其他用户一样,我正在尝试将 UITextField 设置为 UITableViewCell 以用于编辑行。但我没有添加新视图,而是尝试使用附件视图属性。一开始我试过这个...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryView = textField;
        [textField becomeFirstResponder];
}

...并且 UITextField 已正确添加,但我需要再次点击它才能显示键盘。但是,如果我这样做...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell addSubview:textField];
        [textField becomeFirstResponder];
}

...它可以工作,键盘出现了,但是,我没有得到将它作为附件视图的任何其他好处(更容易定位,其他视图在它周围移动)。我想 addSubview: 调用正在改变响应者链或其他东西,并允许我的 UITextField 成为第一响应者,但认为也许有另一种方法可以让我设置单元格的附件视图。谢谢。

4

2 回答 2

0

我讨厌我的解决方案,但它可以工作并且正在寻找更好的方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];            

    CGRect detailTextLabelFrame = cell.detailTextLabel.frame;
    detailTextLabelFrame.origin.x -= 100.0f;
    detailTextLabelFrame.size.width += 100.0f;

    UITextField *textField = [[UITextField alloc] initWithFrame:detailTextLabelFrame];

    cell.detailTextLabel.hidden = YES;
    cell.accessoryView = textField;

    // These are the two lines that made it work, but it feels ugly    
    [cell.accessoryView removeFromSuperview];
    [cell addSubview:cell.accessoryView];

    [cell.accessoryView becomeFirstResponder];
}
于 2011-12-21T03:34:06.430 回答
0

我希望您了解文本字段委托。尝试这种方式....可能会对您有所帮助。

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

if(theTextField == yourtextfieldname) {
    [yourtextfieldname resignFirstResponder];
}

return YES;
}
于 2011-12-21T08:24:13.933 回答