2012-10-11 30 views
3

我有一个UITableView,我想以与联系人应用程序类似的方式工作,有一个编辑按钮,单击时将单元格转换为编辑单元格。UITableView需要看起来像联系人与编辑的位置字段

目前,它们是使用单元格样式'left detail'设置的,我已经覆盖了setEditing方法,可以实现,但我不知道如何转换单元格。

这里的一些其他答案包括:“监视表视图的编辑属性是否发生变化(当按下Edit按钮时)。然后将代码添加到委托方法中,以便以不同方式组合,绘制和缩进单元格视图处于编辑模式。“这正是我想要的,但不知道该怎么做。

- (void)setEditing:(BOOL)flag animated:(BOOL)animated 
{ 
    [super setEditing:flag animated:NO]; 
    if (flag == YES){ 
     // Change views to edit mode. 
     self.textField = [[UITextField alloc] initWithFrame:[_titleLabel frame]]; 
     [self.textField setText:_titleLabel.text]; 
     [self.view addSubview:self.textField];   
    } 
    else { 
     // Save the changes if needed and change the views to noneditable. 
     [_titleLabel setText:self.textField.text]; 
     [self.textField removeFromSuperview]; 
    } 
} 

在我的方法我已经从another question该工程..之类的(它使在错误的地方飞新的可编辑文本字段,并不会隐藏标签)采取代码。 Before edit During edit After edit

apple guidelines是不够具体,我明白如何发展的意见。

+0

什么ü希望...你能否清楚地告诉你的问题...对不起,我没有得到你... – IronManGill

+0

我正在寻找复制联系人应用程序视图,因此当用户单击编辑时,视图中的字段会更新为可编辑的视图。谢谢 –

+0

所以你有编辑tableviewcells的问题? – IronManGill

回答

0

我有一种变通方法。

如果我创建一个自定义行并使其看起来与'left detail'风格相同,但是在右侧使用textview而不是标签,则可以更改“seteditable”和“setenabled”字段视图,以便在编辑时允许编辑。我点击了字体颜色,以便在单击编辑时发生变化,以便用户可以看到它现在可以编辑。

这似乎很杂乱 - 所以我仍然在寻找最好的方法来做到这一点。

- (void)setEditing:(BOOL)flag animated:(BOOL)animated 
{ 
    [super setEditing:flag animated:NO]; 
    if (flag == YES){   
     [self.tableView setEditing:YES animated:NO]; 
     [self.sampleLabel setEnabled:YES]; 
     [self.sampleLabel setTextColor:[UIColor blackColor]]; 
    } 
    else { 
     [self.sampleLabel setEnabled:NO]; 
     [self.sampleLabel setTextColor:[UIColor darkGrayColor]]; 
    } 
} 

- (void)configureView 
{ 
    self.titleLabel.text = [[self.detailItem valueForKey:@"title"] description]; 
    self.ticketNumberLabel.text = [[self.detailItem valueForKey:@"reference"] description]; 
    self.detailsLabel .text = [[self.detailItem valueForKey:@"details"] description]; 
    self.sampleLabel.text = [[self.detailItem valueForKey:@"reference"] description]; 

    // initially set labels to not be editable 
    [self.detailsLabel setEditable:NO]; 
    [self.sampleLabel setEnabled:NO]; 

} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    // item can't be deleted now 
    return NO; 
} 

one two three

0

简而言之,它的工作方式是在整个UITableView上设置编辑标志,然后实现几个在UITableViewDataSource协议中声明的确定正在编辑哪些单元格的方法(canEditRowAtIndexPath,commitEditingStyle)。

所以首先您需要将UITableVIew置于编辑模式。你想要做的是,在处理您的工具栏按钮:

[self.tableView setIsEditing:YES animated:NO];

然后,tableview中会调用canEditRowAtIndexPath,以确定是否该行可以编辑:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

最后,当用户做编辑,调用此方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

这里有一个例子:

http://www.behindtechlines.com/2012/06/02/enabling-configuring-uitableview-edit-mode/

+0

谢谢 - 这可以很好地删除项目,但我不希望这是一个选项,我只是想改变项目的文本,例如门票的标题 –