- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
Now your table view will expect 2 sections with 5 rows each, and try to draw them. Then, in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger actualIndex = indexPath.row;
for(int i = 1; i < indexPath.section; ++i)
{
actualIndex += [self tableView:tableView
numberOfRowsInSection:i];
}
//you can use the below switch statement to return
//different styled cells depending on the section
switch(indexPath.section)
{
case 1://prepare and return cell as normal
default:
break;
case 2://return alternative cell type
break;
}
}
上記の actualIndex
のロジックは、次のようになります。
- セクション1、1からXまでの行はindexPath.rowをそのまま返します。
- セクション2、行1〜YはX + indexPath.rowを返します。
- セクション3、行1〜ZはX + Y + indexPath.rowを返します。
- 任意の数のセクションに拡張可能
基になる配列(または他のフラットコンテナクラス)のアイテムがテーブルセルをバッキングしている場合は、これらのアイテムを使用してテーブルビューに複数のセクションを記入することができます。