初识UITableView

来源:互联网 发布:u盘文件数据恢复 编辑:程序博客网 时间:2024/05/17 22:44
一.是什么

a.在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView ,UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳

b.两种格式:UITableViewStylePlain  和 UITableViewStyleGrouped

c.UITableView需要一个数据源(dataSource)来显示数据,凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源


二.展示数据源的方法

1.调用数据源的下面方法得知一共有多少组数据

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;

2.调用数据源的下面方法得知每一组有多少行数据

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;

3.调用数据源的下面方法得知每一行显示什么内容

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

ps:头尾标题

/** *  第section组显示怎样的头部标题 */- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (section == 0) {        return @"**";    } else if (section == 1) {        return @"**";    } else {        ........    }}/** *  第section组显示怎样的尾部标题 */- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    if (section == 0) {        return @"**";    } else if(section == 1) {        return @"**";    } else {        return @"**";    }}


三.代理方法

行高一致

self.tableView.rowHeight = 60;

行高不一致

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row == 0) return **;    return ...;}


四.cell

1 style

2.可以通过UITableViewCelltextLabeldetailTextLabel和imageView属性访问子控视图

3.重用原理:

当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCelldataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
ps:
有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
bei•解决方案UITableViewCell有个NSString*reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象

4.重用代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 1.定义一个cell的标识      static NSString *ID = @"mjcell";        // 2.从缓存池中取出cell      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];        // 3.如果缓存池中没有cell      if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];    }        // 4.设置cell的属性...          return cell;}

不重用时
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

5.其他
UITableViewCell内部有个默认的子视图:contentViewcontentViewUITableViewCell所显示内容的父视图,可显示一些辅助指示视图
辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCellaccessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:
 UITableViewCellAccessoryNone,                   // don't show any accessory view

 UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track    标示:>

 UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks  标示:圈i 和 >

 UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track               标示:对勾
 
 UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0)                标示:圈i
还可以通过cellaccessoryView属性来自定义辅助指示视图(比如往右边放一个开关)
cell.accessoryView = [[UISwitch alloc] init];

背景:
// 设置背景(背景view不用设置尺寸, backgroundView的优先级 > backgroundColor)    UIImageView *bgView = [[UIImageView alloc] init];    bgView.image = [UIImage imageNamed:@"buttondelete"];   // bgView.backgroundColor = [UIColor redColor];    cell.backgroundView = bgView;        UIView *selectedbgView = [[UIView alloc] init];    selectedbgView.backgroundColor = [UIColor greenColor];    cell.selectedBackgroundView = selectedbgView;





 

0 0