博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发UI之UITableView的基本使用
阅读量:4614 次
发布时间:2019-06-09

本文共 4942 字,大约阅读时间需要 16 分钟。

表格控件

一. 设置数据源<UITableViewDataSource>

1. 设置数据源

- (void)viewDidLoad {    [super viewDidLoad];        // 设置数据源    self.tableView.dataSource = self;        // 设置代理    self.tableView.delegate = self;        // 表格头部和尾部显示    self.tableView.tableHeaderView = [[UISwitch alloc] init];    self.tableView.tableFooterView = [[UISwitch alloc] init];}

2. 设置有多少组数据

/** *  有多少组数据 */- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return self.carsGroup.count;    }

3. 设置每组有多少行数据

/** *  每一组有多少行 */- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    LDCarGroup *carGroup = self.carsGroup[section];    return carGroup.cars.count;}

4. 设置每一行的数据

/** *  每一行的内容 */- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    LDCarGroup *carGroup = self.carsGroup[indexPath.section];        // 缓存池的ID标识    static NSString *ID = @"car";        // 从缓存池中取出可循环利用的cell    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];        // 缓存池中没有可循环利用的cell    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];    }       LDCar *car = carGroup.cars[indexPath.row];        // 1.图像显示    cell.imageView.image = [UIImage imageNamed:car.icon];    // 2.标题文字显示    cell.textLabel.text = car.name;    // 3.子标题显示    cell.detailTextLabel.text = @"Good!!!";        // 4.设置cell右边的指示器    /*     UITableViewCellAccessoryCheckmark : 显示√     UITableViewCellAccessoryDetailButton : 显示详情按钮     UITableViewCellAccessoryDetailDisclosureButton : 显示详情按钮+尖括号     UITableViewCellAccessoryDisclosureIndicator : 显示 尖括号     UITableViewCellAccessoryNone : 默认右边指示器什么也不显示     */    // 4.1 用系统的指示器    cell.accessoryType = UITableViewCellAccessoryNone;        // 4.2定义View显示到cell右边    cell.accessoryView = [[UISwitch alloc] init];        // 5.设置cell的背景色 backgroundView的优先级 > backgroundColor    UIImageView *imageView = [[UIImageView alloc] init];    imageView.image = [UIImage imageNamed:@"btn_left"];    cell.backgroundView = imageView;        // 6.设置选中cell时的背景色    UIImageView *imageView1 = [[UIImageView alloc] init];    imageView1.image = [UIImage imageNamed:@"img_01"];    cell.selectedBackgroundView = imageView1;        return cell;}

5. 组头部描述

/** *  头部描述 */- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    LDCarGroup *carGroup = self.carsGroup[section];    return carGroup.title;}

6. 组尾部描述

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    return  ;  }

7. 表格右边导航条显示

/** *  返回右边导航条显示字符串 */- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    return [self.carsGroup valueForKeyPath:@"title"];}

二. 代理监听UITableView<UITableViewDelegate>

1. 监听选中了哪个cell 

/** *  监听选中了那个cell */int group = 0;int row = 0;- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    LDCarGroup *carGroup = self.carsGroup[indexPath.section];    LDCar *car = carGroup.cars[indexPath.row];        // 设置弹窗    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"车名展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];        // 设置弹窗类型    /*     UIAlertViewStyleDefault : 默认弹窗     UIAlertViewStyleLoginAndPasswordInput : 显示登录对话框     UIAlertViewStylePlainTextInput : 显示一个明文输入框     UIAlertViewStyleSecureTextInput : 显示一个密文输入框     */    alert.alertViewStyle = UIAlertViewStylePlainTextInput;        // 取得文本框,显示车名    [alert textFieldAtIndex:0].text = car.name;        // 显示弹窗    [alert show];        // 获取组号和行号    group = (int)indexPath.section;    row = (int)indexPath.row;}

2. 监听取消选中的cell

/** *  监听取消选中cell */- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section{    }

三. 代理监听UIAlertView弹窗<UIAlertViewDelegate> 

1. 监听点击了UIAlertView弹窗上的哪个按钮

** *  监听点击了UIAlertView弹窗上哪个按钮 */- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    // 1.如果点击左边的取消按钮直接放回    if (buttonIndex == 0) return;        // 2.获取文本框最后的文字    NSString *name = [alertView textFieldAtIndex:0].text;        // 3.修改模型数据    LDCarGroup *carGroup = self.carsGroup[group];    LDCar *car = carGroup.cars[row];    car.name = name;        // 4.刷新数据    // 4.1 全局刷新数据    [self.tableView reloadData];    // 4.2 局部刷新数据    /*     UITableViewRowAnimationFade : 闪一下     UITableViewRowAnimationRight : cell从左边向右滑出刷新     UITableViewRowAnimationLeft : cell从右边向左滑出刷新     UITableViewRowAnimationTop : cell从下向上滑出刷新     UITableViewRowAnimationBottom :     UITableViewRowAnimationNone : 没有什么效果     UITableViewRowAnimationMiddle : cell从上向下滑出刷新     UITableViewRowAnimationAutomatic : 动画闪现     */    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:group];    [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];}

 

转载于:https://www.cnblogs.com/Xfsrn/p/4842397.html

你可能感兴趣的文章
linux 命令大全
查看>>
MongoDB数据库
查看>>
移动端经常出现的兼容问题,谈谈移动端应用或者wap站的一些优化技巧和心得...
查看>>
央行930新政启动房贷证券化 将撬动10万亿进楼市
查看>>
MySql 日期比较大小
查看>>
软件错误
查看>>
gdb
查看>>
tmux使用总结
查看>>
[LeetCode#133]Clone Graph
查看>>
[luogu3980] 志愿者招募
查看>>
转账-AOP基于xml和基于注解
查看>>
Path Sum II
查看>>
ASP.NET SignalR 系列(一)之SignalR介绍
查看>>
Java---字节输入,文件操作,病毒制造,请谨慎运行!
查看>>
Css3在移动设备上的优化点
查看>>
poj 2029 Get Many Persimmon Trees 矩形内部点统计,递推动态规划
查看>>
HDU 2089 不要 62
查看>>
排序算法之——桶排序
查看>>
Linux压力测试工具
查看>>
根据节点的绝对路径创建Xml
查看>>