【问题标题】:Custom Sections in TableViewTableView 中的自定义部分
【发布时间】:2011-08-26 21:42:02
【问题描述】:

我在这个网站上搜索了一些帖子,但没有找到我的确切问题,所以我正在寻找一些建议。我目前有一个带有 TaskList 实体的应用程序,我用它来支持我当前的 TableView。我想根据实体的多个属性创建部分。例如,我有一个“isShared bool”属性和一个“已完成”bool 属性,我想显示部分以将“共享”项目、“未共享”项目和“已完成”项目分组。

在这种情况下瞬态属性会起作用吗?我看到的大多数应用程序只适用于单个属性,所以我一直无法围绕它进行思考。

提前致谢。

【问题讨论】:

    标签: iphone ios uitableview nsfetchedresultscontroller transient


    【解决方案1】:

    我有一个想法,但这可能不是最好的解决方案。你能根据你的布尔属性创建三个包含“isShared”、“notShared”和“completed”对象的数组吗?

    然后在您的 tableview 单元格方法中

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        //Custom your cell for different section here
        switch (indexPath.section)
        {
             //First section is shared item
             case 0:
                  if(cell == nil){
                      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                 //Custom your cell here, for example, you can assign an item in your list to the cell's textlable
                 cell.textLabel.text = [sharedArray objectAtIndex:[indexPath row]];
                 }
                 break;
             case 1:
                  if(cell == nil){
                       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                  //Custom your cell for not shared item here
                  cell.textLabel.text = [notSharedArray objectAtIndex:[indexPath row]];
                  }
                  break;
              case 2:
                  if(cell == nil){
                       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                  //Custom your cell for not shared item here
                  cell.textLabel.text = [CompletedArray objectAtIndex:[indexPath row]];
                  }
                   break;
              default:
                   break;
        }
        return cell;
    }
    

    因此,您在表格视图中获得了三个按 bool 属性分组的部分。如果您更改列表中的一项,即从非共享部分共享一项未共享的项目,那么您可能需要实现一种方法将该对象从 unSharedArray 移动到 sharedArray,然后调用

    [tableView reloadData]
    

    刷新表格视图。

    【讨论】:

      【解决方案2】:

      我会使用带有数组的字典。我会在 TableViewController 的 init 或 viewDidLoad 方法中填充字典。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-25
        • 1970-01-01
        • 2012-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多