UITableView
表视图用来显示数据列表,
它遵守两个协议:
因为
获取配置数据 UITableViewDelegate
获取行数据 UITableViewDataSource
表视图有两种基本样式:
1: 无格式表 UITableViewStylePlain .
每个分组的标题可以自定义样式, 使用了索引, 又叫索引表
2: 分组表 UITableViewStyleGrouped .
分组间有明显的间距, 一个分组表只有一个分组, 分组表不该使用索引
表视图单元格样式:
UITableViewCellStyleDefault, 不含详细文本
UITableViewCellStyleValue1, 单元两端, 详细在后
UITableViewCellStyleValue2
不显示图标, 详细文本标签在文本标签的左侧
UITableViewCellStyleSubtitle 一上一下, 详细在下
创建后先设置协议
@interface ViewController ()<UITableViewDataSource, UITabBarDelegate>
一般会用数组来把数据显示出来
@property (copy, nonatomic)NSArray *fuck;
- (void)viewDidLoad {
[super viewDidLoad];
self.fuck = @[@"A", @"F", @"C", @"U", @"K"];
UITableView *tableView = (id)[self.view viewWithTag:1];
UIEdgeInsets contentInset = tableView.contentInset;
contentInset.top = 20;
[tableView setContentInset:contentInset];
}
最重要的两个属性, 必不可少
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dwarves count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 声明一个静态字符串
static NSString *FuckIdentifier = @"FuckIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FuckIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:FuckIdentifier];
// 如果是nil, 就用标识符字符串手动创建个新的表视图单元
}
cell.textLabel.text = self.dwarves[indexPath.row];
// 从indexPath的row属性获取当前行, 用表的行号从数组中获得对应的字符串, 然后赋给表单元的textLabel.text属性, 最后将表单元返回
return cell;
}
loading