這是很簡單的,按照以下和疑慮的情況下的步驟檢查出UITableView
documentation:
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];
通過界面生成:
- 展開實用程序菜單(右上角的圖標);
- 選擇你的表格視圖(點擊它);
- 點擊屬性檢查器(右上角第四個圖標);
- 在Table View下,單擊樣式下拉列表並選擇分組。
2.實現UITableViewDataSource協議:
基本上這三種功能添加到您的控制器。
- (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定義正在發生的事情出現在單元格的右側,附件類型的列表可以發現here 。
在第一個單元格(藍牙)中,您需要創建一個自定義附件視圖並將其分配給單元的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;
}
希望這有助於馬特烏斯
這是一個完全標準'UITableView'在組模式。你到底在做什麼? –
是的你是對的。謝謝。 – Ali