2013-03-26 40 views
0

我有一個Iphone應用程序,我從NSMutableArray顯示錶格視圖。所有工作正常。現在我需要它在分組表格視圖中顯示兩個部分。因此,我的數組的第一個元素是在首節始終和第二section.When的reming元素我正在做這樣在我的cellForRowAtIndexPath我的第一個單元格在第二section.`漸漸空虛如何在第一部分中顯示我的數組的第一項和在tableview中的下一部分中的剩餘部分?

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的第一部分和下一個的剩餘元素。我想保持它與罪惡陣列。

+0

你的第二部分第一個單元格是空的,我猜是因爲頭要的isEqual @「DDD」 – 2013-03-26 08:14:47

+0

@GuoLuchuan我需要添加下一個元素在該列表中 – hacker 2013-03-26 08:17:19

回答

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

     }  

    } 

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

} 
+0

那是什麼我did.but它沒有顯示你的數組的最後一個元素.. – hacker 2013-03-26 08:11:22

+1

這是因爲在第二部分中,你應該使用[urArray objectAtIndex:indexPath.row + 1]來獲取數組的元素。 – Cosmin 2013-03-26 08:19:39

相關問題