在UITableViewCell中编辑TextLabel [英] Editing TextLabel in a UITableViewCell

查看:38
本文介绍了在UITableViewCell中编辑TextLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何在UITableView中进行就地编辑?

我对这款iPhone的开发并不陌生.我只想知道如何在tableView中编辑/更新UITextLabel.

我已经在使用编辑/完成动画,并且能够删除行,但是我无法获得如何编辑这些行中的文本.

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

我已经搜索了该站点,但无法获得确切答案.

 <代码>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];如果(!单元格){cell = [[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault默认Identifier:@"CellIdentifier"]自动释放];cell.editingAccessoryType = YES;}myTextField = [[UITextField alloc] initWithFrame:CGRectMake(15,6,241,31)];myTextField.backgroundColor = [UIColor clearColor];myTextField.textAlignment = UITextAlignmentLeft;myTextField.returnKeyType = UIReturnKeyDone;myTextField.delegate =自我;cell.accessoryView = myTextField;myTextField.text = [self.arrayOfItems objectAtIndex:indexPath.row];myTextField.enabled =否;返回单元}-(IBAction)(id)发件人{如果(自行编辑){[myTextField.Enabled = YES];[super setEditing:否动画:否];[self.tab setEditing:NO动画:NO];[self.tab reloadData];[self.navigationItem.rightBarButtonItem setTitle:@完成"];[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];}别的{myTextField.enabled =是;[super setEditing:是动画:是];[self.tab setEditing:YES animation:YES];[self.tab reloadData];[self.navigationItem.rightBarButtonItem setTitle:@编辑"];[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];}}-(UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{如果(self.editing == NO ||!indexPath)返回UITableViewCellEditingStyleNone;如果(self.editing&& indexPath.row ==([[self.arrayOfItems count]))){返回UITableViewCellEditingStyleInsert;}别的{返回UITableViewCellEditingStyleDelete;}返回UITableViewCellEditingStyleNone;} 

这对我不起作用.如果我按下编辑"按钮,文本字段将消失

解决方案

在表格行中编辑文本本身很困难,因为您需要管理键盘并向上滚动可编辑字段以为键盘等腾出空间(这可能在编辑过程中在屏幕上滚动等).同样,如果您要使每个单元格都可编辑,则还必须管理一个编辑后的值,使其能够在屏幕上进行中编辑等操作(例如,在键盘处于活动状态时等等)滚动显示.

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

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

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

这将使您在编辑字段时最轻松/最佳地控制键盘行为和编辑行为.

Possible Duplicate:
How to do edit-in-place in a UITableView?

I am new to this iphone developing. I just want to know how to edit/update UITextLabel in a tableView.

I am already using edit/done animation and able to delete rows but i am not able to get how to edit the text in those rows.

I want the user to edit the textlabel of the cell.when edit button is tapped.

i already searched the site but couldnt get the exact answer.

-(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;
}

This is not working for me.textfield is disappearing if i hit edit button

解决方案

Editing text in the table rows themselves is difficult because you need to manage the keyboard and scrolling the editable field up to make room for the keyboard etc. (that could be scrolled off screen while being edited etc.). Also if you make every cell editable you have to managed an edited value being able to be scrolled off screen mid-edit etc. (i.e. while the keyboard is active and so forth).

A UILabel is not editable, you need a UITextField to edit text.

The best way to achieve an edit of a value in a table cell is probably to push a new view controller onto the stack on the edit action and have an editable text field there and add save/cancel bar button items to the menu of that view controller.

When the user presses save, update the model behind the table view with the appropriate text value and pop the view controller off the stack again. Have the main view containing the table call reloadData on the tableView in its viewWillAppear method.

This will give you easiest/best control of the keyboard behaviour and editing behaviour when the fields are being edited.

这篇关于在UITableViewCell中编辑TextLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆