简体   繁体   English

TableView中的自定义节

[英]Custom Sections in TableView

I have scoured some posts on this site but haven't found my exact issue so I am looking for some advice. 我已经搜索了该站点上的一些帖子,但是没有找到确切的问题,因此我正在寻找一些建议。 I currently have an app with a TaskList entity which I use to back my current TableView. 我目前有一个带有TaskList实体的应用程序,用于备份当前的TableView。 I would like to create sections based on multiple attributes of the entity. 我想基于实体的多个属性创建部分。 For example, I have a "isShared bool attribute and a "completed" bool attribute and I would like to display sections to group "shared" items, "not shared" items and "completed" items. 例如,我有一个“ isShared bool属性和一个“ completed” bool属性,并且我想显示将“共享”项目,“非共享”项目和“已完成”项目分组的部分。

Is this a situation where the transient property would work? 这是瞬态属性起作用的情况吗? Most of the applications I have seen only apply to a single attribute, so I haven't been able to wrap my brain around it. 我见过的大多数应用程序仅适用于单个属性,因此我无法将其包围。

Thanks in advance. 提前致谢。

I have an idea in my head, but this might not be the best solution. 我脑子里有个主意,但这可能不是最好的解决方案。 Can you create three arrays containing objects for "isShared", "notShared" and "completed" based on your bool attributes? 您是否可以根据布尔属性创建三个数组,分别包含“ isShared”,“ notShared”和“ completed”对象?

Then in your tableview cell methods 然后在您的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;
}

So you get three sections in table view grouped by the bool attributes. 因此,您将在表视图中获得按bool属性分组的三个部分。 If you change one item in your list, ie shared one unshared item from unshared section, then you might need to implement a method to move that object from unSharedArray to sharedArray, then call 如果更改列表中的一项,即从unshared部分共享了一个unshared项,则可能需要实现一种方法,将该对象从unSharedArray移到sharedArray,然后调用

[tableView reloadData]

to refresh the table View. 刷新表视图。

I would use a dictionary with arrays. 我会使用带有数组的字典。 I would fill the dictionary in the init or the viewDidLoad method of the TableViewController. 我将字典填充到TableViewController的init或viewDidLoad方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM