1.UITableView
UITableView继承于UIScrollView,表视图的每一条数据都显示在UITableViewCell对象中,表视图可以分区显示数据,每个分区成为一个section,每一行为row,编号都是从零开始.
2.表视图的创建
<span style="font-size:18px;">//初始化一个UITableView
self.tableView = [[UITableView alloc]initWithFrame:self.frame style:UITableViewStyleGrouped];
//separatorStyle:表视图的分割线样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//表视图分割线的颜色
self.tableView.separatorColor = [UIColor redColor];
//rowHeight:表视图的行高(设置行高一般使用协议中的方法)
self.tableView.rowHeight = 100;
</span>
3.表视图显示
3.1表视图显示需要遵循两个协议,即UITableViewDataSource和UITableViewDelegate这两个协议.
其中在UITableViewDataSource中有了两个必须实现的方法.
第一个必须实现的方法.
<pre name="code" class="objc"><span style="font-size:18px;">//设置(每一个)section中row的数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
</span>
第二个必须实现的方法.
<span style="font-size:18px;">//显示每一行的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//初始化一个UITableViewCell()
self.tableCell = [tableView dequeueReusableCellWithIdentifier:@"cell_id"];
if (nil == self.tableCell) {
self.tableCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell_id"];
}
self.tableCell.textLabel.text = [self.array3[indexPath.section][indexPath.row]valueForKey:@"name"];
self.tableCell.detailTextLabel.text = [self.array3[indexPath.section][indexPath.row]valueForKey:@"Tel"];
self.tableCell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[self.array3[indexPath.section][indexPath.row]valueForKey:@"image"]]];
self.tableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// self.tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
return self.tableCell;
}
</span>
以下的方法为常用方法(不是一定要实现)
<span style="font-size:18px;">//设置UItableView的分组数目
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}</span>
<span style="font-size:18px;">//重新加载数据(重新加载数据调用reloadData方法)
-(void)viewDidDisappear:(BOOL)animated{
[self.rootView.tableView reloadData];
}</span>
<span style="font-size:18px;">
//实现跳转和传值
</span><pre name="code" class="objc"><span style="font-size:18px;">-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_detailVC.image = self.tableCell.imageView.image;
_detailVC.stringName = self.tableCell.textLabel.text;
_detailVC.stringTel = self.tableCell.detailTextLabel.text;
[self.navigationController pushViewController:self.detailVC animated:YES];
}</span>
<span style="font-size:18px;">//设置cell的header的距离
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
//向UITableViewCell的header中添加视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:tableView.tableHeaderView.frame];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(222, 20, 100, 20)];
label.text = @"Cocoa";
[view addSubview:label];
if (section == 0) {
return view;
}else{
return 0;
}
}
//向UITableViewCell中的foot中添加视图
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return 0;
}
</span>
4.表视图的重用机制
UITableView是靠mutableSet来实现重用功能
出屏幕的cell会被添加到mutableSet中,进屏幕的cell,先从set中获取,如果获取不到,才创建一个cell.在cell显示之前,给cell赋上相应的内容.cell的reusableIdentify是重用的关键.
//UITableViewCell的重用
self.tableCell = [tableView dequeueReusableCellWithIdentifier:@"cell_id"];
if (nil == self.tableCell) {
self.tableCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell_id"];
}