0

我有一个 Iphone 应用程序,我在其中显示来自 . 的表格视图NSMutableArray。一切正常。现在我需要将它显示在分组表格视图的两个部分中。所以我的数组的第一个元素在第一部分总是和第二部分中的 reming 元素。当我在我的 cellforrowatindexpath 中这样做时,我的第一个单元格在第二部分中变得空了。

NSMutableDictionary *dicttable=[self.array objectAtIndex:indexPath.row];
   NSString *head=[[dicttable objectForKey:@"message"] description];

 if(indexPath.section==0)
 {
    if([head isEqualToString:@"ddd"])
    {
        label.textAlignment = UITextAlignmentLeft;
        label.textColor =[UIColor darkGrayColor];
        label.font = [UIFont fontWithName:@"Helvetica" size:16];

        label.text = head;
        label.tag=100;
        [cell.contentView addSubview:label];
     }
  }
  else
  {
     if(![head isEqualToString:@"ddd"])
     {
        label.textAlignment = UITextAlignmentLeft;
        label.textColor =[UIColor darkGrayColor];
        label.font = [UIFont fontWithName:@"Helvetica" size:16];

        label.text = head;
        label.tag=100;
        [cell.contentView addSubview:label];

     }      

 }

我在第一部分需要这个 ddd,在下一部分需要剩下的元素。我想继续使用单数组。

4

2 回答 2

2

尝试这个

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

       NSInteger row = indexPath.section!=0?indexPath.row+1:indexPath.row;
       NSMutableDictionary *dicttable=[self.array objectAtIndex:row];
       NSString *head=[[dicttable objectForKey:@"message"] description];
        if(indexPath.section==0)
     {
        if([head isEqualToString:@"ddd"])
        {
            label.textAlignment = UITextAlignmentLeft;
            label.textColor =[UIColor darkGrayColor];
            label.font = [UIFont fontWithName:@"Helvetica" size:16];

            label.text = head;
            label.tag=100;
            [cell.contentView addSubview:label];
         }
      }
      else
      {
         if(![head isEqualToString:@"ddd"])
         {
            label.textAlignment = UITextAlignmentLeft;
            label.textColor =[UIColor darkGrayColor];
            label.font = [UIFont fontWithName:@"Helvetica" size:16];

            label.text = head;
            label.tag=100;
            [cell.contentView addSubview:label];

         }      

     }

  }
于 2013-03-26T08:25:46.443 回答
1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
              return 1;
    return [yourArray count] - 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (section == 0) {
              // Your first section

        }
    else {
           // Your second section
        }

}
于 2013-03-26T08:09:12.413 回答