ios - 带有部分重复单元格的 UITableView

标签 ios objective-c uitableview

我正在尝试将部分标题添加到我的 UITableView。我能够创建这些部分并正确计算每个部分中的元素数量,但是当我调用我的 cellForRowAtIndexPath 方法时,该表会重复两个部分中的数据。

这是我准备 UITableView 部分内容的地方:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    int sections = 0;
    if (sectionOneCount>0) {
        sections++;
    }

    if (sectionTwoCount>0) {
        sections++;
    }
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return sectionOneCount;
    }
    else {
        return sectionTwoCount;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        return @"Section One Title";
    }
    else {
        return @"Section Two Title";
    }
}

这是我的 cellForRowAtIndexPath 方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"MainCell";

    ProfileTableViewCell *cell = [mainTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.label1.text = [[items objectAtIndex:indexPath.row] valueForKey:@"label1"];
    cell.label2.text = [[items objectAtIndex:indexPath.row] valueForKey:@"label2"];

    return cell;
}

关于我在这里做错了什么的任何想法?谢谢!

附言这是我正在使用的一些示例数据。我只有两个可能的部分,每种类型一个(在本例中为红色和蓝色)。我有一个名为 items 的主数组(如您在我的 cellForRowAtIndexPath 方法中所见)。
{
    "parentArray": {
        "id": 5,
        "items": [
            {
                "type": "red",
                "title": "RedItem1"
            },
            {
                "type": "red",
                "title": "RedItem2"
            },
            {
                "type": "blue",
                "title": "BlueItem1"
            },
            {
                "type": "blue",
                "title": "BlueItem2"
            },
            {
                "type": "blue",
                "title": "BlueItem3"
            }
        ]
    }
}

最佳答案

要继续使用您的逻辑...

假设您的 sectionOneCountsectionTwoCount正确且顺序正确,您可以使用 sectionOneCount作为填充第 2 节内容的偏移量。

例子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MainCell";

    ProfileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(indexPath.section == 0) {
        cell.label1.text = items[indexPath.row][@"label1"];
        cell.label2.text = items[indexPath.row][@"label2"];
    }
    else if (indexPath.section == 1) {
        cell.label1.text = items[indexPath.row + sectionOneCount][@"label1"];
        cell.label2.text = items[indexPath.row + sectionOneCount][@"label2"];
    }

    return  cell;
}

PS:使用storyboard时,不需要if (cell == nil)堵塞

或者...

假设您的 items看起来像
{
  "items": [// array of sections
    [//array of section 1 items
      {
        "type": "red",
        "title": "redItem1"
      },
      {
        "type": "red",
        "title": "redItem2"
      }
    ],
    [// array of section 2 items
      {
        "type": "blue",
        "title": "blueItem1"
      },
      {
        "type": "blue",
        "title": "blueItem2"
      },
      {
        "type": "blue",
        "title": "blueItem3"
      }
    ]
  ]
}

基本上:
{
  "items": [//array of 2 things
    [ /*array of all redItems*/ ],
    [ /*array of all blueItems*/ ]
  ]
}

这将是:
  • 可扩展,您可以轻松地添加一个新的 greenItems数组和更多
  • 删除对 sectionCount 的任何依赖变量
  • 内部颜色项目的顺序无关紧要

  • 而且...您的主要委托(delegate)方法只是:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return items.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [items[section]].count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"MainCell";
    
        UITableView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        //no need for if-else anymore as the following is dynamic enough
    
        cell.label1.text = items[indexPath.section][indexPath.row][@"label1"];
        cell.label2.text = items[indexPath.section][indexPath.row][@"label2"];
    
        return  cell;
    }
    

    PS:即使您使用更多部分更新了数据源,也不需要触及此代码。

    关于ios - 带有部分重复单元格的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25832018/

    相关文章:

    ios - Segue 在 Swift 3 中从上到下过渡

    iphone - 反向地理编码和本地化

    iphone - 自定义允许此应用程序使用位置弹出窗口 iOS

    ios - 快速拉动刷新

    ios - UITableView单元格插入失败

    ios - 如何在自定义 UITableView 单元格上获取 UITextField 值

    ios - NSURLConnection 和 NSMutableURLRequest 认证

    iphone - 重用 UILabel 样式的正确方法是什么?

    objective-c - 当 UIImageView 图像完成加载时停止动画 UIActivityIndi​​catorView

    ios - 主控/详细信息 - 如果对象被删除或主控中没有留下任何对象,则清除详细信息 (iOS)