我想在我的 iPhone 应用程序中创建这样的 View :
我不知道iOS中这个控件到底是什么,也许我可以在左侧设置一个图标和文本,在右侧设置一个小标志。
我已经实现了一个 TableView,在那里我可以设置这些东西,就像这样:
[[cell textLabel] setText:customer.name];
[[cell textLabel] setTextColor:[UIColor blackColor]];
[[cell imageView] setImage:[UIImage imageNamed:@"icon.png"]];
//[[cell detailTextLabel] setText:@"Awsome weather idag"];
cell.accessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
但是我怎样才能让它像图片中的那样工作呢?
最佳答案
这很简单,按照以下步骤操作,如有疑问,请查看UITableView
documentation :
<强>1。创建分组 TableView :
以编程方式:
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];
通过界面构建:
- 展开实用程序菜单(右上角图标);
- 选择您的表格 View (点击它);
- 点击属性检查器(右上角第四个图标);
- 在表格 View 下,点击样式下拉菜单并选择分组。
<强>2。实现 UITableViewDataSource 协议(protocol):
基本上将这三个函数添加到您的 Controller 。
- (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 的默认样式有一个 ImageView (UIImageView)、一个标题标签(UILabel)和一个附属 View (UIView)。您只需要在您提供的图像中复制表格 View 。
因此,您正在 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 .
在第一个单元格(蓝牙)中,您需要创建自定义附件 View 并将其分配给单元格的 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;
}
希望这有帮助,马特乌斯
关于ios - 分组的 UITableView - 复制设置应用 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12773597/