非常に簡単です。以下の手順に従ってください。疑問がある場合は、UITableView
ドキュメントを確認してください。
1.グループ化されたテーブルビューを作成します。
プログラムで:
CGRect tableFrame = CGRectMake(0, 0, 200, 200);
UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewGroupedStyle];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
UITableViewControllerサブクラスの割り当て(一般的なケース):
MyTableViewController *controller [[MyTableViewController alloc] initWithStyle: UITableViewGroupedStyle];
[self.navigationController pushViewController:controller animated:NO];
インターフェイスビルドを介して:
- ユーティリティメニュー(右上隅のアイコン)を展開します。
- テーブルビューを選択します(クリックします)。
- 属性インスペクター(右上隅の4番目のアイコン)をクリックします。
- [テーブルビュー]で、[スタイル]ドロップダウンをクリックし、[グループ化]を選択します。
2.UITableViewDataSourceプロトコルを実装します。
基本的に、この3つの関数をコントローラーに追加します。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in a given section.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cells.
return cell;
}
3.セルの構成:
UITableViewCellのデフォルトのスタイルには、画像ビュー(UIImageView)、タイトルラベル(UILabel)、およびアクセサリビュー(UIView)があります。提供した画像にテーブルビューを複製するために必要なものはすべてあります。
だから、あなたはこのようなものを探していますtableView:cellForRowAtIndexPath:
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * const cellIdentifierDefault = @"default";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierAccount];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierAccount];
}
if (indexPath.section == 0) {
cell.imageView.image = [UIImage imageName:@"bluetooth_icon"];
cell.textLabel.text = @"Bluetooth";
// Additional setup explained later.
}else{
if (indexPath.row == 0) {
cell.imageView.image = [UIImage imageName:@"general_icon"];
cell.textLabel.text = @"General";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}else{
cell.imageView.image = [UIImage imageName:@"privacy_icon"];
cell.textLabel.text = @"Privacy";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
return cell;
}
プロパティaccessoryTypeは、セルの右側に表示される内容を定義します。アクセサリタイプのリストは、ここにあります。
最初のセル(Bluetooth)で、カスタムアクセサリビューを作成し、それをセルのaccessoryViewプロパティに割り当てる必要があります。これを実現する方法の非常に素朴な例を以下に示します。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * const cellIdentifierDefault = @"default";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierAccount];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierAccount];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
label.textAlignment = NSTextAlignmentRight;
cell.accessoryView = label;
}else{
label = (UILabel *) cell.accessoryView;
}
cell.imageView.image = [UIImage imageName:@"bluetooth_icon"];
cell.textLabel.text = @"Bluetooth";
label.text = @"Off";
return cell;
}
これがお役に立てば幸いです、マテウス