hacker
hacker

Reputation: 8957

How to display the first item of my array in the first section and the remaing in the next section in a tableview?

I have an Iphone application in which i am displaying a table view from an NSMutableArray.All are working fine.Now i need it to be displayed in two sections in a grouped table view.So that the first element of my array is in the first section always and the reming elements in the 2nd section.When i am doing like this in my cellforrowatindexpath my first cell is getting empty in the second section.`

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];

     }      

 }

I need this ddd in the first section and the remaing elements in the next. I would like to keep it doing with the sinle array.

Upvotes: 0

Views: 65

Answers (2)

Anupdas
Anupdas

Reputation: 10201

Try this

    - (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];

         }      

     }

  }

Upvotes: 2

Cosmin
Cosmin

Reputation: 2908

- (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
        }

}

Upvotes: 1

Related Questions