ios - UITableView 需要看起来像 Contacts with edit in place fields

标签 ios iphone uitableview ios6

我有一个 UITableView,我想以与“联系人”应用类似的方式工作,因为它有一个编辑按钮,单击该按钮会将单元格转换为编辑单元格。

目前它们是使用单元格样式“左侧细节”设置的,我已经覆盖了准备好实现的 setEditing 方法,但我不知道如何转换单元格。

这里的其他一些答案包括“监视 TableView 的编辑属性何时更改(当按下编辑按钮时)。然后将代码添加到您的委托(delegate)方法中,以不同的方式组合、绘制和缩进单元格,当 TableView 处于编辑模式。”这正是我想要的,但不知道该怎么做。

- (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不够具体,我无法理解如何形成观点。

最佳答案

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

因此,首先您需要将 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/

关于ios - UITableView 需要看起来像 Contacts with edit in place fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12839850/

相关文章:

ios - 在 IOS 7 中扩展表格 View 单元格

ios - 为什么 tableView 不是单例?

ios - 在 swift3 中使用 OneSignal sendTags 发送数据时出现语法问题

ios - 在另一个 View 中嵌入自定义 View 的正确 MVC 结构是什么?

c# - MonoTouch : SQLite-net thread safety and SQLiteConfig. 序列化

ios - 将单元格数据(即 cell.textlabel.text 和 cell.imageView.image)传递给另一个 View Controller

ios - JSON 文件直到 UITableView 才加载

ios - Swift 检测并设置 wkwebview 以尊重硬件静音开关

ios - 几个小部件使用 XCode Storyboard(或 "doing it wrong")?

c# - 仅在特定 html 文件上从 uiwebview 中删除表单助手 (Phonegap)