【问题标题】:UITableView Dynamic SectionUITableView 动态部分
【发布时间】:2013-09-12 13:11:11
【问题描述】:

我正在使用一个 tableview,它使用动态部分,我有一个 sectionArray,它由部分标题和另一个数组组成,其中包含所有部分的全部数据, 我无法弄清楚如何为每个部分的行编写单元格,因为它是动态的

代码是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return [appDelegate.sectionArray count];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
  return [appDelegate.appNameArray count];
}

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

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  UILabel *AppNameLabel=[[UILabel alloc]initWithFrame:CGRectMake(80, 0, 120, 30)];
  [AppNameLabel setBackgroundColor:[UIColor clearColor]];
  [AppNameLabel setText:[NSString stringWithFormat:@"%@",[appDelegate.appNameArray objectAtIndex:indexPath.row]]];
  [AppNameLabel setFont:[UIFont systemFontOfSize:14]];
  [cell.contentView addSubview:AppNameLabel];
  return cell;
}

在 appname 数组中,我有包含所有部分的数据,我需要为特定部分隔离,如何在单元格中制作行索引请帮助。

提前致谢。

【问题讨论】:

    标签: ios iphone ipad uitableview


    【解决方案1】:

    无需为 sectionTitle 和 appName 创建两个不同的array。使用 NSDictionary 的对象创建源 NSArrayNSDictionary 对象包含 appName 数组为values,sectionTitle 为key

    sourceArray
    (
     dictionaryObject1
     {
     key : sectionTitle,
     value : ( appName1,appName2,appName3)
     },
     dictionaryObject2
     {
     key : sectionTitle,
     value : ( appName1,appName2,appName3)
     },
    .
    .
    .
    )
    

    【讨论】:

      【解决方案2】:

      首先,您的 numberOfRowsInSection 方法应该如下所示:

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
          NSArray *arrayForSection = appDelegate.sectionArray[section];
          return [arrayForSection count];
      }
      

      之后修改cellForRow方法:

      NSArray *arrayForSection = appDelegate.sectionArray[indexPath.section]
      [AppNameLabel setText:[NSString stringWithFormat:@"%@",[arrayForSection objectAtIndex:indexPath.row]]];
      

      【讨论】:

      • 它将如何根据部分进行区分?我的意思是,我将部分数组作为我的日期(9 月 12 日),并且在 appnamearray 中包含 2013-09-12 13:07:03 Appname,2013-09-11 13:07:03 Appname,2013-09-10 13:07 :03 Appname etc....它将如何根据部分名称分配?
      • 嗯,您需要研究使用数据结构的方式。您需要有一个主数组(用于部分),它将被其他数组填充 - 每个部分一个,包含该部分中行的信息。
      【解决方案3】:

      您需要相应地更新您的数据源。只需通过枚举您的数组来动态编辑您的数组。就像您添加了一个部分一样,您应该在数组中动态添加部分详细信息。

      NSArray *tempArray = [NSArray alloc] initWithArray:sectionArray];
      for(int i = index; i<= (sectionArray.count-i-1); i++)
      {
         if(i = index){
            [sectionArray replaceObjectAtIndex:i withObject:newSection]
         }
         else
         {
           sectionArray replaceObjectAtIndex:i withObject:[tempArray objectAtIndex: i-1];
         }
      }
      

      【讨论】:

      • 我是否应该在我的单元格中为行或节数实现此功能?
      • 我刚刚给了你这个想法..这段代码将更新你的sectionArray,如果你想区分不同部分的行,那么你应该制作一个包含部分名称的字典数组,然后包含单独的字典每个部分的数组。
      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      相关资源
      最近更新 更多