【问题标题】:UITableView header title with undefined number of sectionsUITableView 标题标题与未定义的节数
【发布时间】:2013-05-06 12:23:06
【问题描述】:

如果我有一个 UITableView,它有 [arrayofStudents count] 部分:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [arrayofStudents count]
}

然后我想为每个部分做一个标题标题(注意部分的数量与arrayofStudent的大小有关)

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

       switch (section) {
       case //if it is the first section :
            return [arrayofStudents objectAtIndex:0];
            break;
        case //if it is the second section:
            return [arrayofStudents objectAtIndex:1];
            break;
        //case until arrive to the count of arrayofStudents
        default:
            break;
}

case 语句的数量与 arrayofStudents 的数量有关,如果我在 arrayofStudents 中有 10 个元素,那么我将有 10 个部分。 如何在 switch case 语句中构建它?

感谢您的帮助。

【问题讨论】:

    标签: ios uitableview ios6 switch-statement


    【解决方案1】:

    你可以使用

    [arrayofStudents objectAtIndex:section];
    

    希望对你有所帮助。

    【讨论】:

    • @androniennnthanx 接受 :)
    【解决方案2】:

    case 语句看起来是多余的:因为 section 和 NSArrays 都是从零开始索引的,所以同样可以使用

    return [arrayofStudents objectAtIndex:section];
    

    也许在开始时需要一些额外的参数检查来处理默认值。

    【讨论】:

      【解决方案3】:

      使用这个:

      -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
                  return [arrayofStudents objectAtIndex:section];
      
      }
      

      希望对你有帮助

      【讨论】:

      • 简单到我没想到:/
      • 公平地说,我标记了第一个(正确)答案,即使你的答案更清楚。对不起,伙计。
      • 没有问题。但如果你看到回答时间,那么我在 Divz 之前回答了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 2013-03-14
      • 2023-03-14
      • 2018-10-14
      相关资源
      最近更新 更多