Iphone 在UITableViewCell中编辑文本标签

Iphone 在UITableViewCell中编辑文本标签,iphone,ios,uitableview,Iphone,Ios,Uitableview,可能重复: 我对这款iphone是新手。 我只是想知道如何在tableView中编辑/更新UITextLabel 我已经在使用“编辑/完成”动画并能够删除行,但我无法了解如何编辑这些行中的文本 我希望用户在点击编辑按钮时编辑单元格的文本标签 我已经搜索了这个网站,但没有得到确切的答案 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UI

可能重复:

我对这款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;
}

这对我不起作用。如果我按编辑按钮,文本字段将消失。如果您使用的是标准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];

在自定义单元格中使用UITextField代替UILabel,然后可以进行编辑。对于自定义单元格,可以选中此项
或者您也可以检查这一点。

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

UILabel不可编辑,您需要UITextField来编辑文本

编辑表格单元格中的值的最佳方法可能是将新的视图控制器推到“编辑”操作的堆栈上,并在其中具有可编辑的文本字段,然后将保存/取消栏按钮项添加到该视图控制器的菜单中

当用户按save时,使用适当的文本值更新表视图后面的模型,并再次将视图控制器弹出堆栈。让包含表调用的主视图在其ViewWillExample方法中重新加载tableView上的数据


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

这不是他想要的。我想要编辑/更新文本标签。因此,您想要更改代码中
UITableCell
textlab
,或者希望用户能够编辑单元格文本?我想要用户若要编辑单元格文本,请在单元格(textlabel)上添加透明按钮,并为其添加选择器。在选择器open alertview with textfield for edit中,将新文本保存到数据模型,刷新表视图。有很多方法可以解决这个问题。您是否尝试将textfield添加到单元格?我想在按下“编辑”按钮时动态编辑textLabel。您可以这样做。提示:不要运行“tableView重新加载数据”因为当你转换到编辑模式时,这将覆盖单元格动画。我不知道如何做。你可以发布一些代码吗?我已经编辑了我的答案,并为你提供了一个如何自定义TableViewCell的链接。如果你从我的答案中得到了解决方案,那么就接受它。我已经有一个textlabel,当用户点击编辑按钮时,textlabel应该被删除可编辑。您不能编辑UILabel文本。根据您的要求,您可以将UILabel替换为UIExtField。虽然这并不能真正回答如何进行表内文本编辑的问题,但它确实提出了一些有效的观点,说明了为什么在所有情况下都不适合使用UILabel。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
.........

cell.textLabel.text = @"my text";

return cell;
}