【问题标题】:Possible to tag a section in UITableView?可以在 UITableView 中标记一个部分吗?
【发布时间】:2011-08-18 19:40:23
【问题描述】:

我有一个 uitableview 的单元格,其行为取决于什么:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  if (indexPath.section == 2) {
    do x;
   }else{
    do y;
  }   
}

当您处理单个表时,它有点工作,但是当您尝试添加部分或子类时,这种类型的“幻数”很容易破坏。

我的问题是,是否可以“标记”该部分?所以我们不做section==1,而是做:

indexPath.section.tag=="user_stats" {load x}
indexPath.section.tag=="answers" {show answers}
indexPath.section.tag=="page" {show pagination}

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    是的,只需在某处设置一个枚举。

    enum {  kSectionUserStats = 0,
            kSectionAnswers,
            kSectionPage };
    

    然后:

    if(indexPath.section == kSectionPage)
    {
        // do x
    } else if(indexPath.section == kSectionAnswers)
    {
        // do y
    }
    // etc.
    

    这也让您只需更改它们在枚举中的顺序,就可以非常轻松地重新排序您的部分。

    【讨论】:

      【解决方案2】:

      如果您正在显示带有标题的部分标题,那么标题本身可以成为您的标签。

      if([self titleForHeaderInSection:indexPath.section] == @"MySection1")
      {
           //do something
      }
      else
      {
           //do something else
      }
      

      【讨论】:

      • Objective-C 中没有关于字符串比较的内容吗? [string1 isEqualToString:string2]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2011-07-19
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多