UITableView的基本使用"/>
UITableView的基本使用
UITableView作为iOS开发里基本控件,是我们第七个需要学习的。下面我来为大家介绍一下UITableView的一些常用属性和它们的用法。
这里附上UI控件演示的源码地址:。源码持续更新中...
因为部分代码是从Xcode直接拷贝出来的样式有误请谅解。
UITableView的内容较多,这里我就只演示基本代码,关于tableView的自定义cell和多样式的不同之处,以及插入和删除,请下载源码。
//初始化一个plan样式的tableView
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"ID"];
[self.view addSubview:self.tableView];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
headerView.backgroundColor = [UIColor redColor];
//添加整个tableView的header
self.tableView.tableHeaderView = headerView;
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
footerView.backgroundColor = [UIColor greenColor];
//添加整个tableView的footer
self.tableView.tableFooterView = footerView;
//设置索引字体颜色
// self.tableView.sectionIndexColor = [UIColor blackColor];
//设置索引背景颜色
// self.tableView.sectionIndexBackgroundColor =[UIColor blackColor];
下面是我们UITableView的一些代理方法:
显示每组标题索引
//
//-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//
//}
//
返回每个索引的内容
//
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//
//}
//
响应点击索引时的委托方法
//
//-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
//
//}
//返回tableView有多少组,不写默认返回1组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
//返回tableView每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
//返回一个cell,用来显示具体内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ID"];
}
cell.textLabel.text = [[NSString alloc] initWithFormat:@"第%ld组,第%ld行",(long)indexPath.section,(long)indexPath.row];
return cell;
}
//返回没行有多高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
}
//返回每组的header高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 35;
}
//返回每组的footer的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 35;
}
//设置section的header的具体内容
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ID"];
UILabel *label = [header viewWithTag:101];
if (label == nil) {
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 35)];
label.textColor = [UIColor blackColor];
label.font = [UIFont systemFontOfSize:16];
label.tag = 101;
[header addSubview:label];
}
label.text = [[NSString alloc] initWithFormat:@"第%ld组的header",(long)section];
return header;
}
//设置section的footer的具体内容
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ID"];
UILabel *label = [header viewWithTag:101];
if (label == nil) {
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 35)];
label.textColor = [UIColor blackColor];
label.font = [UIFont systemFontOfSize:16];
label.tag = 101;
[header addSubview:label];
}
label.text = [[NSString alloc] initWithFormat:@"第%ld组的footer",(long)section];
return header;
}
在这里我们只是简单的介绍了plan样式的使用,关于自定义样式,插入,删除等操作请下载源码查看,如果还需要tableView更多的功能请留言,我将完善这些功能。
这里附上UI控件演示的源码地址:。源码持续更新中...
写在结尾:本文初衷是提供给一些新手或者需要某些资料查询者,代码或者思想有不足之处请大家谅解,希望大家多多原谅。最后希望大家可以共同进步,快乐工作。
上一篇文章: 《UIScrollView的基本使用》更多推荐
UITableView的基本使用
发布评论