【问题标题】:iPhone: replacing a cell's view with UITextFieldiPhone:用 UITextField 替换单元格的视图
【发布时间】:2010-10-09 10:24:03
【问题描述】:

我基本上是在尝试在 tableView:commitEditingStyle:forRowAtIndexPath: 上将 UITextField 添加到 UITableViewCell。在插入时,我想在现有单元格上添加一个 UITextField。

我用这样的方式设置了 textField:

-(void) setUpIndexField {
    inputField = [[UITextField alloc] init];
    inputField.textAlignment = UITextAlignmentCenter;
    inputField.borderStyle = UITextBorderStyleRoundedRect;    
    inputField.keyboardType = UIKeyboardTypeNamePhonePad;
    inputField.delegate = self;
 }

然后在commitEditingStyle中,我有这样的东西:

- (void)tableView:(UITableView *)tv
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSInteger row = [indexPath row];
    if (editingStyle == UITableViewCellEditingStyleInsert) {
    if (row == 0) {
        UITableViewCell *cell = [tv cellForRowAtIndexPath:indexPath];
        inputField.frame = [tv cellForRowAtIndexPath:indexPath].frame;
        [cell.contentView addSubview:inputField];
        [inputField becomeFirstResponder];
        return;
    }
// more code etc.
}

当我单击插入访问器 (+) 时,textField 确实会弹出到屏幕上,但在单击它的单元格附近没有。它进入 UIView 的随机部分,而不是单击 + 的 indexPath 的实际单元格。

了解我出错的地方以及如何将 UITextField 放置到单击 + 的单元格上是理想的。

【问题讨论】:

    标签: ios objective-c iphone cocoa cocoa-touch


    【解决方案1】:

    您正在将inputField 的框架设置为不会执行您想要的操作的单元格的框架。单元格内的视图使用框架的左上角为0,0。你的文本框应该是:

    inputField.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
    

    因为这是相对于它将放置文本字段的超级视图,所以它完全覆盖了该单元格中的其他视图。

    【讨论】:

    • 嗨 Squeegy,差不多就是这样。现在唯一的另一个问题是单元格的宽度是 320,因此当 UITableView 的样式被舍入时,它会延伸到单元格之外。有没有办法让它保持在 UITableViewCell 的范围内,之后不再继续?
    • 当然。把宽度和高度变短,原点x和y增加一点。只需调整值直到合适为止。
    猜你喜欢
    • 2015-03-07
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2011-05-26
    • 1970-01-01
    相关资源
    最近更新 更多