零基础iOS开发学习日记-控制器篇-UITableViewController

开头

UITableViewController

实际用处

  • 微博推文界面
  • 微信、QQ的好友界面,聊天界面
  • 新闻界面
  • 只要是界面上需要列举内容,而且每块内容需要的数据,都大致相同,就可以用到

基础用法

  • UITableViewController数据的加载也是通过UITableViewDataSource的代理方法实现的
  • 头尾也可以返回
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
//组内行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
//cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.text = @"test";
    return cell;
}
//头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"Header%ld", section];
}
//尾
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"Footer%ld", section];
}
复制代码

UITableView

表单样式

UITableViewStylePlain //表头和表尾是悬浮的
UITableViewStyleGrouped //常用,表头和表尾可以随界面滚动
UITableViewStyleInsetGrouped //内嵌样式
复制代码
  • 设置UITableViewController的表单样式
- (instancetype)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleInsetGrouped];
}
复制代码
  • 在界面上添加UITableView
self.view = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
复制代码

一些设置

  • 设置分割线
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
复制代码
  • 分割线颜色
self.tableView.separatorColor = [UIColor redColor];
复制代码
  • 渲染颜色
self.tableView.tintColor = [UIColor orangeColor];
复制代码

数据刷新

  • 当遇到更新表单中的数据时,需要数据刷新
//刷新全部
[self.tableView reloadData];
//刷新某一组
NSIndexSet *idxSet = [NSIndexSet indexSetWithIndex:1];
[self.tableView reloadSections:idxSet withRowAnimation:UITableViewRowAnimationFade];
//刷新某一行
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:1];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
复制代码

UITableViewCell

  • 在实际开发中,UITableViewCell一般是自定义的,但是要求不高的时候也会选择用系统的cell类型
UITableViewCellStyleDefault //图片+标题
UITableViewCellStyleValue1 //图片+标题+右注释
UITableViewCellStyleValue2 //标题+左注释
UITableViewCellStyleSubtitle //图片+标题+下注释
复制代码

基本设置

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.textLabel.text = @"test";
cell.imageView.image = [UIImage imageNamed:@"dv_icon1"];
cell.detailTextLabel.text = @"detail";
复制代码

一些设置

  • 指示符
cell.accessoryType = UITableViewCellAccessoryDetailButton;
UITableViewCellAccessoryNone
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailButton
复制代码
  • 选中形式,blue和gray设置后的样式和default一样,不知道什么原因
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault
cell.selectionStyle = UITableViewCellSelectionStyleNone;
复制代码

高度

  • 实际开发中,每个cell的高度不尽相同,可以在自定义的模型中,计算好高度属性
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
复制代码

应用场景的功能

  • 滚动到指定位置,例如,点击QQ或者微信的输入框,界面自动滚到最后一条消息。滚动有四个位置,但是我尝试了,没有什么区别
[self.tableView scrollToRowAtIndexPath:idxPath //滚动的列和组
atScrollPosition:UITableViewScrollPositionTop //滚动的位置
animated:YES];
复制代码
  • 选中某一行的处理,例如跳转到不同的界面,下面是,根据文本,跳转到界面,将NSString转换成类
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *targetVC = cell.textLabel.text;
    Class class = NSClassFromString(targetVC);
    UIViewController *vc = [class new];
    [self presentViewController:vc animated:YES completion:nil];
}
复制代码
  • 这里顺带写一下,将NSString转换为方法名
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *targetFunc = cell.textLabel.text;
SEL funcName = NSSelectorFromString(targetFunc);
[self performSelector:funcName];
复制代码
  • 带参数,如果还要多参数,可以用字典传
//调用
[self performSelector:funcName withObject:@"name" withObject:@18];
//函数
- (void)testViewController:(NSString *)name andAge:(NSNumber *)age {
    NSLog(@"testViewController %@ %d", name, [age intValue]);
}
//cell的内容
cell.textLabel.text = @"testViewController:andAge:";
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享