【问题标题】:Editable tableHeaderView in UITableView (like Contacts App)UITableView 中的可编辑 tableHeaderView(如联系人应用程序)
【发布时间】:2010-02-12 14:09:30
【问题描述】:

我目前正在尝试使用分组的 UITableView 实现可编辑的详细信息视图。我希望它看起来像联系人应用程序:

  • 在查看状态下,它应该将标题显示为普通标签(在联系人中,它是具有透明背景的名称)。
  • 在编辑状态下,它应该将标题显示为可编辑的 UITableViewCell(在 Contact 的 tableHeader? 中,从仅具有透明背景的纯文本变为具有白色背景的标准 UITableViewCell)。

我不确定实现这一目标的最佳方法是什么。首先,我尝试将标题添加为 UILabel tableHeaderView(效果很好),但随后我无法将其切换到 UITableViewCell。一种可能是在进入编辑模式时删除标题并添加一个新部分。

目前我正在尝试始终使用 UITableViewCell 并使其在查看模式下透明并在编辑模式下将其切换为默认值。但是,我无法使 UITableViewCell(在 UITableViewCellStyleDefault 中)的 UILabel 透明(尽管我确实设法使 UITableViewCell 透明,但不是其中的 textLabel)。

实现此行为的最佳方法是什么?

【问题讨论】:

    标签: iphone uitableview editing contacts


    【解决方案1】:

    我也这样做了(尽管 iOS4 中联系人应用程序的更改尚无定论!)我的解决方案使用两个不同的标题视图并根据 isEditing 在它们之间切换:

    - (UIView *)infoHeaderAnimated:(BOOL)animated {
        UIView *header = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 90.0)] autorelease];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(98.0, 41.0, 221.0, 21.0)];
        label.font = [UIFont boldSystemFontOfSize:17.0];
        label.backgroundColor = [UIColor clearColor];
        label.text = baseEntity.labelText;
        [header addSubview:label];
        [label release];
        return header;
    }
    
    - (UIView *)editingHeaderAnimated:(BOOL)animated {
        UIView *header = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 90.0)] autorelease];
        UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(78.0, 10.0, 240.0, 90.0) style:UITableViewStyleGrouped];
        tv.backgroundColor = [UIColor clearColor];
        tv.dataSource = self;
        tv.delegate = self;
        tv.rowHeight = 62.0;    //@@@ height of cell and frame depend on elements
        tv.tag = kEditingHeaderTag;
        editingHeaderTableView = [tv retain];
        [header addSubview:tv];
    [tv release];   
        return header;
    }
    

    【讨论】:

      【解决方案2】:

      你要做的很标准,考虑在UITableViewDatasource中实现这些协议,尤其是titleForHeaderInSection & commitEditingStyle:

      Configuring a Table View
      – tableView:cellForRowAtIndexPath:  required method
      – numberOfSectionsInTableView:
      – tableView:numberOfRowsInSection:  required method
      – sectionIndexTitlesForTableView:
      – tableView:sectionForSectionIndexTitle:atIndex:
      – tableView:titleForHeaderInSection:
      – tableView:titleForFooterInSection:
      
      Inserting or Deleting Table Rows
      – tableView:commitEditingStyle:forRowAtIndexPath:
      – tableView:canEditRowAtIndexPath:
      

      记得在 Interface Builder 中将 TableView 的类型选择为 Group 而不是 Plain。

      【讨论】:

      • 感谢您的回答。但是,联系人应用程序似乎使用 tableHeader 而不是 sectionHeader 作为顶视图,并将其更改为编辑模式下的标准单元格。或者它使用透明单元格,在编辑模式下切换为正常。
      • 啊,你说的是详细查看一个联系人,点击修改?我认为这里没有什么特别的魔力,第一个标题是一个普通的 tableview 单元格,但是具有自定义样式,它还设置了这个单元格的高度以使感觉不同,但实际上并非如此。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多