2011-07-08 21 views
0

私は3つのセクションを持つテーブルを持っています、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.rowを取得するためにindexPath.sectionを使用しています。これらのプロパティは、カテゴリNSIndexPath (UIKitAdditions)で定義されています。

+0

ok cool私はこれを使用し、私は動作していると思うが、私が下にスクロールしようとしているときに、次のセクションに表示されるものを見ると、クラッシュする。私がやっていることは間違っていますか?コードについては上記を参照してください。 – Ashutosh

+0

@Ashutosh、明らかに間違ったことは何も見ません。クラッシュが発生した場所でエラーと行を表示するのに役立ちますが、おそらくこれが新たな質問領域に迷い込んでいる可能性があります。 – Caleb

+0

申し訳ありません...私のせいです。 allSectorsはNSSetを内部に持つNSMutableArrayです。修正:cell.textLabel.text = [[self.allSectors objectAtIndex:indexPath.row] valueForKey:@ "SECTORNAME"]; – Ashutosh

関連する問題