【问题标题】:Editing TextLabel in a UITableViewCell [duplicate]在 UITableViewCell 中编辑 TextLabel [重复]
【发布时间】:2012-02-09 11:01:46
【问题描述】:

可能重复:
How to do edit-in-place in a UITableView?

我是这个 iphone 开发的新手。 我只想知道如何在 tableView 中编辑/更新 UITextLabel。

我已经在使用编辑/完成动画并且能够删除行,但我不知道如何编辑这些行中的文本。

我希望用户在点击编辑按钮时编辑单元格的文本标签。

我已经搜索了该网站,但无法得到确切的答案。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if(!cell)
{
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"]autorelease];
    cell.editingAccessoryType=YES;
}


myTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 6, 241, 31)];
myTextField.backgroundColor = [UIColor clearColor];
myTextField.textAlignment = UITextAlignmentLeft;
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.delegate = self;
cell.accessoryView = myTextField;
myTextField.text = [self.arrayOfItems objectAtIndex:indexPath.row];
myTextField.enabled = NO;
return cell;
}

-(IBAction)Edit:(id)sender
{
if(self.editing)
{
            [myTextField.Enabled = YES];
    [super setEditing:NO animated:NO];
    [self.tab setEditing:NO animated:NO];
    [self.tab reloadData];
    [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else 
{
    myTextField.enabled= YES;

    [super setEditing:YES animated:YES];
    [self.tab setEditing:YES animated:YES];
    [self.tab reloadData];
    [self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
 }
 }

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
if (self.editing == NO || !indexPath) 
return UITableViewCellEditingStyleNone;
if (self.editing && indexPath.row == ([self.arrayOfItems count])) 
{
    return UITableViewCellEditingStyleInsert;
} 
else 
{
    return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}

这对我不起作用。如果我点击编辑按钮,文本字段就会消失

【问题讨论】:

    标签: iphone ios uitableview


    【解决方案1】:

    编辑表格行本身的文本很困难,因为您需要管理键盘并向上滚动可编辑字段以为键盘等腾出空间(在编辑等时可能会滚动到屏幕外)。此外,如果您使每个单元格都可编辑,您必须管理一个已编辑的值,以便能够在编辑过程中滚动到屏幕外等(即在键盘处于活动状态时等等)。

    UILabel 是不可编辑的,你需要一个 UITextField 来编辑文本。

    在表格单元格中编辑值的最佳方法可能是在编辑操作时将新的视图控制器推送到堆栈上,并在那里有一个可编辑的文本字段,并将保存/取消栏按钮项添加到菜单那个视图控制器。

    当用户按下保存时,使用适当的文本值更新表格视图后面的模型,然后再次将视图控制器从堆栈中弹出。让包含表的主视图在其 viewWillAppear 方法中对 tableView 调用 reloadData。

    这将使您在编辑字段时最容易/最好地控制键盘行为和编辑行为。

    【讨论】:

    • 虽然这并不能真正回答如何进行表内文本编辑的问题,但它确实提供了一些有效的观点,说明为什么它在所有情况下都不是一个好主意。
    【解决方案2】:

    在您的自定义单元格中使用 UITextField 代替 UILabel,然后您可以编辑。对于自定义单元格您可以检查此 Tutorial 或者你也可以检查这个Apple Sample

    【讨论】:

    • 我不知道该怎么做。你能发布一些代码吗?
    • 我已经编辑了我的答案,并为您提供了如何自定义 TableViewCell 的链接
    • 如果您从我的回答中得到解决方案,请接受。
    • 我已经有一个文本标签,当用户点击编辑按钮时,文本标签应该是可编辑的。
    • 你不能编辑 UILabel text.for 你的要求你可以用 UITextField 替换 UILabel。
    【解决方案3】:

    如果您使用的是标准 UITableViewCell,您可以执行以下操作:

    //Get the first cell of the first section
    NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:ip];
    
    //Change the textLabel contents
    [[cell textLabel] setText@"Updated text"];
    
    //Refresh the tableView
    [tableView reloadData];
    

    【讨论】:

    • 我想在按下编辑按钮时动态编辑 textLabel。
    • 这确实可以让你做到这一点。提示不要运行“tableView reloadData”,因为当您转换到编辑模式时,它将覆盖单元格动画。
    【解决方案4】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    .........
    
    cell.textLabel.text = @"my text";
    
    return cell;
    }
    

    【讨论】:

    • 这不是他想要的。
    • 我想编辑/更新 textLabel。
    • 所以您想在代码中更改UITableCelltextLabel,或者您希望用户能够编辑单元格文本?
    • 我希望用户能够编辑单元格文本
    • 在单元格(文本标签)上添加透明按钮,为其添加选择器。在选择器中打开带有文本字段的警报视图进行编辑,将新文本保存到数据模型,刷新表格视图。有很多方法可以解决这个问题。您是否尝试将文本字段添加到单元格?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多