UITableView的使用(类似android中ListView)
(1)拖动UITableView到布局中
(2)为UITableView设置UITableViewDataSource数据源(类似android中的Adapter),
首先:
代码方式:
self.tableView.dataSource = self; //在viewDidLoad方法中
或
拖线方式:
右击View Controller -> View -> 右击UITableView,在弹出框中将dataSource拖动到View Controller上。
然后:@interface ViewController () <UITableViewDataSource> //实现UITableViewDataSource方法
//重写此方法,控制列表显示几行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 总行数; //总共显示行数
}
//重写此方法,控制列表每组显示几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ section表示当前组position
return 行数; //每组显示几行
}
//重写此方法,每组每行显示什么单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
//对每行View进行重用(类似android中convertView)
UITableViewCell *cell = [tableView dequeueReusableCellWithIndentifier:@"tag值"]; //标识当前行的唯一标识
if(cell == nil){ //为null就创建新的
//indexPath.section表示当前组position,indexPath.row表示当前行position
cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tag值"]; //标识当前行的唯一标识,自定义item的View时用xib的tag值,用xib自定义item的布局时需要设置行高
}
cell.textLable.text = @"每行标题"; //UITableViewCell自带的靠上的UILablel,里面还有个UIImageView和UILable
cell.imageView.image = [UIImage imageNamed:@"图片名"];
cell.detailTextLabel.text = @"描述";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //右边显示小箭头
cell.accessoryView = 自定义的View; //右边箭头改成想要的自定义View
cell.backgroundColor = [UIColor blueColor]; //设置当前行背景色
cell.selectedBackgroundView = UIView引用; //设置当前行选中背景色,需要创建选中效果的View进行赋值
return cell;
}
使用Main.storboard中的模版当作cell
(1)在Main.storboard中的UITableView上拖一个item的View,并设置identity属性为自定义tag值,并设置Class属性为继承了UITableViewCell的自定义类
(2)重写以下方法,当缓存中无此cell时会自动创建
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
//对每行View进行重用(类似android中convertView),可以自定义UITableViewCell类
UITableViewCell *cell = [tableView dequeueReusableCellWithIndentifier:@"自定义tag值"]; //为上面指定的自定义tag值
//设置数据,可以自定义UITableViewCell,并将设置数据放在此类中
return cell;
}
//为每组设置头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section {
if(section == 0){//判断当前组position来设置不同的头标题
return "头标题名";
}
}
为每组设置头View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section {
static NSString *tag = @"tag名称";
UITableViewHeaderFooterView *headerV = [UITableView引用 dequeueReusableHeaderFooterViewWithIdentifier:tag]; //根据tag从缓存中获取item的组View
if(headerV == nil){
headerV = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:tag]; //创建
}
headerV.contentView = 自定义View; //自定义View需要指定位置与宽高
//给各子View设置数据
return headerV;
}
当控件的frame发生改变时触发layoutSubviews方法
- (void)layoutSubviews{
[super layoutSubviews];
self.bounds; //(就是frame)可以获取当前View的宽高等值
}
设置每组头View高度
UITableView引用.sectionHeaderHeight = 高度值;
//为每组设置尾标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger) section {
if(section == 0){//判断当前组position来设置不同的尾标题
return "尾标题名";
}
}
//统一设置UITableView行高
self.UITableView引用.rowHeight = 高度值;
//设置不同行不同高度
首先:@interface ViewController () <UITableViewDelegate>
其次:
- (CGFloat)tableView:(UITableView *)tableVeiw heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//根据条件返回不同的高度值
return 高度值;
}
(3)滚动时固定组标题在最上面,并隐藏状态栏
- (BOOL)prefersStatusBarHidden{
return YES;
}
UITableView常见属性
rowHeight:设置每行的高度
separatorColor:分隔线的颜色
separatorStyle:分隔线样式
值:
UITableViewCellSeparatorStyleNone; //无分隔线
tableHeaderView:列表头部View
tableFooterView:列表底部View
设置UITableView右侧索引,点击时跳到指定组(只要实现以下方法)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return @[@"A", @"B"]; //返回字符串数组
}
//监听UITableView的item选中方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
//刷新UITableView整个列表数据
[self.UITableView引用 reloadData];
//刷新UITableView某个组
NSIndexSet *pos = [NSIndexSet indexSetWithIndex:pos位置值];
[UITableView引用 reloadSections:pos withRowAnimation:UITableViewRowAnimationLeft]; //引处为左移动画,UITableViewRowAnimationFade为淡入淡出
//刷新UITableView局部数据
NSIndexPath *index = [NSIndexPath indexPathForRow:@"某一行tag名称" inSection:0]; //一个对象行
NSIndexPath *index = [NSIndexPath indexPathForRow:@某一行position inSection:0]; //一个对象行
[self.UITableView引用 reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic]; //刷新当前行数据
将plist文件中的资源赋值给实体类中同名的属性(plist中key名必须与属性名一致)
[self setValuesForKeysWithDictionary:dict]; //self为当前实体类对象
监听滚动事件
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollV{
...
}
使用UITableView静态单元格(数据不变的情况下使用,UITableView必须放在UITableViewController中使用)
(1)设置为静态单元格
Content属性设置为Static Cells,Style属性设置为Basic;
(2)设置组数
选中UITableView,Sections属性输入行数
(3)设置每组的行数
选中某一组,Rows属性输入行数
(4)设置单元格内容(选中某行)
Image属性输入资源中的图片名称 //设置最左边图标
Accessory属性设置为Disclosure Indicator //显示最右边小箭头
选中中间的文本框,输入指定文字
设置UITableView的FooterView
UITableView引用.tableFooterView = View引用;
设置UITableView的HeaderView
UITableView引用.tableHeaderView = View引用;
设置UITableView背景色
UITableView引用.backgroundColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1.0]; //alpha设置透明度
设置UITableView中的item不能被点击
UITableView引用.allowsSelection = NO;
iOS:UITableView使用
最新推荐文章于 2023-05-18 14:13:48 发布