1. UITableView的介绍
- 实现表格展示,最常用的做法就是使用UITableView ,UITableView 继承自UIScrollView ,因此可以支持垂直滚动.
- 分为两个样式 : plain (普通样式) grouped(分组样式)
- UITableView 需要一个数据源来显示数据, 会向数据源查询一共有多少行,多少组,以及每一行显示什么数据
凡事遵循了UITableViewDateSource的协议的OC对象,都可以是他的数据源
设置 组数 -(NSInteger)numberOfSectionIntableView:(UITableView *)tableView 设置 组内行数 必须实现 -(NSInteger)tableview:(UITableView * )tableView numberOfRowsInSection:( NSInteger )section ; 设置 行内容 必须实现 -(UITableviewcell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ;
1.1 初始MVC
mvc是一种设计思想,贯穿于整个ios开发中,需要积累一定的项目经验.才能深刻体会其中的含义和好处
mvc 的三个角色
1. M: model, 模型数据(字典转模型)
2. V: View 视图 (界面)
3. C: Control, 控制中心
View上面显示什么东西,取决于Model ,只要Model数据改了,View的显示状态会跟着更改,Control负责初始化Model.并将Model传递View去解析展示
1.2 UITableViewCell的结构
含有一个 contentView 蒙版, contentView 下默认有3 个子视图
其中2个是UILabel ( 通过UITableViewCell 的 textLabel 和 detailTextLabel 属性访问) 第三个是UIInageView ( 通过 imageView 属性来访问)
UITableViewCell 还有一个UIableViewCellStyle 属性 ,用来决定使用contentView 的那些子视图,以及这些子视图在cell中显示的位置
UITableViewCellStyleDefault 图片 Text Label
UITableViewCellStyleSubtitle 图片 Text Label + DeTailText Label (下面)
UITableViewCellStyleValue1 图片 Text label + DeTailText Label (左面)
UITableViewCellStyleValue2 TextLabel + detailText Label
在cell的自定义中 : 添加控件到self.contentView中
2. 通知机制
2.1 创建通知对象
notificationWithName 通知的名字
//object: 发布的对象
NSNotification *notification = [NSNotification notificationWithName:@"yule" object:sinaNews userInfo:@{@"奥斯卡":@"小李子终于不是打酱油的,拿到小金人了"}];
2.2 注册监听对象
Observer 监听的对象
selector:@selector(<#selector#>) 代表监听的对象,接收到消息以后所执行的方法
name :通知的名字->如果不限定 就传入nil
object 通知的发布对象 -->如果不限定发送通知的对象,就传入nil
[notificationCenter addObserver:p1 selector:@selector(showNotifaction:) name:nil object:sinaNews];
2.3 发布通知
- 获取通知中心
单利设置模式,拿到的永远是同一个对象->只要应用程序运行就永远不会被释放
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
发布通知
[notificationCenter postNotification:notification3]; [notificationCenter postNotificationName:@"junshi" object:wangNews userInfo:@{@"朝鲜":@"三胖用的电脑 苹果电脑"}];
2.4 注销通知
- (void)dealloc{
//ARC 非ARC -> MRC
//在非ARC中一定不要忘记
//[super dealloc];
//销毁监听对象
[[NSNotificationCenter defaultCenter]removeObserver:self]; }
发送消息 回复消息
滚动到最后一行
//6. 自动滚动到UITableView的最后一行 scroll
//6.1 创建NSIndexPath对象
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messageFrameArray.count - 1 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];