2011-07-08 42 views
0

我有一個表有3個部分,所有三個部分有不同數量的行。單元格值應該從不同的數組中填充。 那麼我應該如何找出它在方法cellForRowAtIndexPath中的哪個部分:???如何在單個表視圖的不同部分中顯示不同數組中的不同元素?

請幫忙!!!

Code: 

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section 
{ 
    currentSection = section; 
    int number = 0; 
    switch (section) { 
     case 0: 
      number = self.basic.count; 
      break; 
     case 1: 
      number = allSectors.count; 
      break; 
     case 2: 
      number = 1; 
      break; 
     default: 
      number = 0; 
      break; 
    } 

    return number; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease]; 

    } 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    CGFloat fontSize = [UIFont systemFontSize]; 
    CGFloat smallFontSize = [UIFont smallSystemFontSize]; 
    switch (indexPath.section) { 
    case 0: 
     cell.textLabel.text = [self.basic objectAtIndex:indexPath.row]; 
     break; 
    case 1: 
     cell.textLabel.text = [self.allSectors objectAtIndex:indexPath.row]; 
     break; 
    case 2: 
     cell.textLabel.text = @"None"; 
     break; 

    default: 
     break; 
} 


return cell; 
} 

回答

1

只需使用indexPath.section得到的錶款,並indexPath.row讓行中的那一節。這些屬性在類別NSIndexPath (UIKitAdditions)中定義。

+0

好酷。我用這個,我想它的工作,但當我試圖向下滾動,看看在下一節顯示什麼,它崩潰。我在做什麼錯事?請參閱上面的代碼。 – Ashutosh

+0

@Ashutosh,我沒有看到任何明顯的錯誤。這將有助於發生事故發生的錯誤和線路,但也許這正在陷入新的問題領域。 – Caleb

+0

對不起......我的錯。 allSectors是一個NSMutableArray,裏面有NSSet。修復:cell.textLabel.text = [[self.allSectors objectAtIndex:indexPath.row] valueForKey:@「SECTORNAME」]; – Ashutosh

相關問題