美文网首页
iOS开发之UI(十)

iOS开发之UI(十)

作者: zero点点 | 来源:发表于2016-02-22 22:12 被阅读54次

1.UITableView的概念

  • UITableView继承于UIScrollView,可滚动
  • UITableView的每一条数据对应的单元格叫做Cell,是UITableViewCell的一个对象,继承于UIView
  • UITableView可以分区显示,每一个分区称为section,每一行称为row,编号都是从0开始
  • 系统提供了一个专门的类来整合section和row,叫做NSIndexPath

2.UITableView的基本使用

系统提供了一个类UITableViewController,用于方便管理UITableViewController

UITableView显示的相关属性

self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;// 分割线样式
self.tableView.separatorColor = [UIColor redColor];// 分割线颜色
self.tableView.tableHeaderView 置顶视图
self.tableView.tableFootView 置底视图

3.UITableView显示数据

遵守UITableViewDelegate与UITableViewDataSource

  • dataSource,显示数据相关代理
  • detegate,视图操作相关代理

// 设置dataSource代理
tableView.delegate = self;
// 设置delegate代理
tableView.dataSource = self;

然而TableViewController中已经自带协议并且已经设置好代理,所以不用重复以上代码

必须实现的两个协议方法
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell )tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

3.UITableViewCell

首先要注册Cell

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];// 注册UITableViewCell

然后实现方法

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{
   // 从重用池中获取cell
   UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"reuse"] autorelease];
   cell.textLabel.text = @"标题";
   return cell;
}

4.重用机制

解决问题:如果tableView要显示超过100条数据,需要多少个cell?内存毕竟超过每个App的规定,造成闪退

重用cell的流程
1.当一个cell被滑出屏幕,这个cell会被程序放到相应的重用池中
2.当tableView需要显示一个cell,会先去重用池中尝试获取一个cell
3.如果重用池没有cell,就会创建一个cell
4.取得cell之后会重新赋值进行使用

5.UITableView常用方法

// 返回分区头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"头部标题";
}

// 返回底部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return @"底部标题";
}

// 返回UITableView右侧索引目录
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return @[@"1"];
}

// 告诉delegate选中了一个cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"选中");
}

// 每一个分区的顶部高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

// 每一个分区的底部高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

// 每一个分区的顶部自定义视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

}

// 每一个分区的底部自定义视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

}

相关文章

  • UI常用的控件

    #iOS开发之UI篇#iOS开发之UI篇 #常用控件介绍1## #UI第09天:滚动视图# ##UIScrollV...

  • KVC

    iOS 如何使用KVC iOS开发UI篇—Kvc简单介绍 iOS开发系列--Objective-C之KVC、KVO

  • iOS开发之UI(十)

    1.UITableView的概念 UITableView继承于UIScrollView,可滚动 UITableVi...

  • IOS学习(9)-UITabBarController

    iOS开发UI篇—UITabBarController简单介绍视图之UITabBarController结构详解(...

  • iOS部分控件介绍及设计规范

    iOS及Android图标按钮设计规范 UI设计师需要了解的开发中常用的UI控件(ios篇) iOS交互设计基础之...

  • 2019-03-22

    iOS 开发之修改图片image颜色 吐槽:平时开发中可能因为 UI妹子懒给到图片中没有需要的颜色,或者嫌弃UI...

  • UITableView ──分页加载

    IOS开发UI展示之UITableView ──分页加载 在ios开中中,由于屏幕尺寸限制,如果需要显示的数据很多...

  • iOS开发之定位

    iOS开发之定位 iOS开发之定位

  • UI第一周学习总结

    ios开发之UI学习第一周总结 UIView基本属性、方法、视图关系、动画 基本属性和方法 UIView:是iOS...

  • iOS开发-UI 从入门到精通(二)

    iOS开发-UI 从入门到精通(二)是对 iOS开发-UI 从入门到精通(一)知识点的巩固,主要以习题练习为主,增...

网友评论

      本文标题:iOS开发之UI(十)

      本文链接:https://www.haomeiwen.com/subject/wbybkttx.html