私たちは、セグメント方式の名称を使用することができ、セグメント名が代わりに使用の役割の選手はのUITableViewの良いセグメントの使用を説明するために最も直接的な出発点に、髪をセグメント化され、次のセクションに表示されます。この例では、ヘッダは、次の文を提出、ここでのセクションでは、親クラスとして使用することができたときに、SimpleTableViewControllerセクションに基づいてSectionTableViewControllerという名前のクラスを作成し、SimpleTableViewControllerから継承されます。
#importを " HBSimpleTableViewController.h " @interface HBSectionTableViewController:HBSimpleTableViewController @end
それは役割カテゴリとなるように、データソースの改善を行うことが必要です
- (ボイド)initData { // SimpleTableViewのデータソースの妥当性を認識 【スーパーinitData]; //は、新しいデータソース終了 NSMutableArrayのarrSectionDatasource * = [NSMutableArrayのarrayWithCapacity:0 ]; //は、現在のセクションの名前を記録に使用されてきた NSMutableArrayのをarrSection = * [NSMutableArrayのarrayWithCapacity:0 ]; // 一時的に名前が全てのプレイヤオブジェクトのセグメント記憶 NSMutableArrayのarrTmp * = [NSMutableArrayのarrayWithCapacityを:0 ]; // すべてのプレイヤートラバース するため(* onePlayer HBPlayerInfo でself.datasource) { NSStringの *役割= onePlayer.role。 // 現在のプレーヤーのみセクション名としての役割を行っている場合、続行 //はこの選手が最後のデータソースに追加されたことを意味する IF ([arrSection containsObject:役割]) { 続行; } // 新規役割 // 反復再びプレーヤー のため(* HBPlayerInfoのroleplayer でself.datasource){ IF ([役割rolePlayer.role isEqualToStringとして]) { ; [roleplayer arrTmpのaddObjectは] } } // この役割は、セグメント名の通りである、完成 [arrSectionのaddObject:役割]; // arrTmpは、現在のすべてのセグメント名ロールプレイヤオブジェクトを満たす含ま // 最終的なデータソースに追加します [arrSectionDatasourceのaddObject:arrTmp]; // arrTmpリセット // 全てのミックスに追加満たすプレイヤオブジェクトの新しい役割を待つ arrTmp = [NSMutableArrayのarrayWithCapacity:0 ]; } // データソースをリセット IF (_datasource){ _datasource = ゼロ; } _datasource = [[NSArrayのALLOC] initWithArray:arrSectionDatasource]; }
(1)のUITableView合計SimpleTableViewControllerこのメソッドを実装していない前、段落に分割既定の期間に、マルチセグメントのコードは以下の通りである告げます。
- (NSInteger)numberOfSectionsInTableView:(のUITableView * )のtableView { 戻りself.datasource.count。 }
(2)のUITableViewのセクション名を各セグメントに教えます。各サブアレイのコンテンツの役割は同じ要素であるので、最初の要素は、アクセスを取ることができます
- (NSStringの*)のtableView:(のUITableView * )のtableView titleForHeaderInSection:(NSInteger)セクション { HBPlayerInfo * onePlay = [((にNSArray *)[self.datasource objectAtIndex:セクション])objectAtIndex:0 ]。 返すonePlay.role; }
次のようにテーブルビュー「の生産ライン」コールバック関数の再アクセスに新しいデータソースが、変更されました
- (NSInteger)のtableView:(のUITableView * )のtableView numberOfRowsInSection:(NSInteger)セクション { リターン((にNSArray * )[self.datasource objectAtIndex:セクション])カウント;。 } - (のUITableViewCell *)のtableView:(のUITableView *)のtableView cellForRowAtIndexPath:(NSIndexPath * )indexPath { 静的 NSStringの* CellIdentifier = @ " sectionTableViewCellId " 。 UITableViewCell *セル= [dequeueReusableCellWithIdentifierのtableView:CellIdentifier]。 もし(セル== NIL) { 細胞 =[のUITableViewCell ALLOC] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]。 } // 取得每个球员的方法和SimpleTableViewController有所不同 HBPlayerInfo * onePlayer = ゼロ、 NSArrayの * arrSectionPlayer = [self.datasource objectAtIndex:indexPath.section]; もし(arrSectionPlayer && arrSectionPlayer.count> indexPath.row) { onePlayer = [arrSectionPlayer objectAtIndex:indexPath.row]。 } であれば(onePlayer){ cell.textLabel.text = onePlayer.name。 } 戻りセル。 }
次のようにプログラム実行の効果は次のとおりです。
ます。https://www.cnblogs.com/haibosoft/p/3668709.htmlで再現