2011-04-03 77 views
1

我正在處理一個tableview,在其單元格中顯示多行文本。我想要兩個部分,我已經完成了。但是在第二部分顯示另一個數組是否可能不一樣?牛逼關於分割你的UITableView

這是我使用的代碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
      if (section == 0) { 
      return MIN([titles count], [subtitles count]); 
      } 
      else { 
       return 1; 
      } 
     } 

回答

1

當然,你可以做到這一點。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) { 
     return MIN([titles count], [subtitles count]); 
    } 
    else { 
     return MIN([titles2 count], [subtitles2 count]); 
    } 
} 

只要確保在所有UITableViewDataSource和UITableViewDelegate方法中切換數組。


但我會建議嵌套數組。它使代碼更短,因爲當你有超過10個部分時,你的代碼將變得漫長而醜陋。

而對於嵌套數組,您只有在viewDidLoad或無論您初始化您的數組中有長的難看的代碼;

titles = [NSArray arrayWithObjects:titles0, titles1, nil]; 
subtitles = [NSArray arrayWithObjects:subtitles0, subtitles1, nil]; 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return MIN([[titles objectAtIndex:section] count], [[subtitles objectAtIndex:section]count]); 
} 
+1

我不得不改變的cellForRowAtIndexPath到\t'如果(indexPath.section == 0) \t { \t \t cell.textLabel.text = [標題objectAtIndex:indexPath.row]; \t \t cell.detailTextLabel.text = [subtitles objectAtIndex:indexPath.row]; \t} \t別的 \t \t如果(indexPath.section == 1) \t \t { \t \t \t cell.textLabel.text = [titles2 objectAtIndex:indexPath.row]; \t \t \t cell.detailTextLabel.text = [subtitles2 objectAtIndex:indexPath.row]; \t \t} \t \t return cell; '但現在一切正常。謝謝:) – mstottrop 2011-04-03 12:08:22

+1

多數民衆贊成在我的意思是'只要確保在所有UITableViewDataSource和UITableViewDelegate方法切換數組.' :-) – 2011-04-03 12:42:31