UITableView初级使用与理解
2016-08-17 22:46
323 查看
UITableView在我们项目中使用的频率大家都知道是多么的频繁,所有小蒲在这里慢慢的手打下,我的博客基本都是手打的除了非常长的协议这些我command + c或v,其他基本都是手打的,一方面增加记忆,另一方面练练打字的速度,其他就不多说了,下面我们代码搞起
1:UITableView的初始化
UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.view.bound),CGRectGetHeight(self.view.bounds))];
tableview.dataSouce = self;
tableview.delegete = self;
[self.view addSubView:tableview];
以上就是简单的初始化方法,基本我项目的懒加载里面就是这几句,我们看到我们遵守了协议,tableview协议里面有二个我们必须实现的方法。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return:返回uitableview的行数
}
//此方法用于跟cell赋值和cell的重用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}
这里讲下还有几个经常用的方法
//定义分组的组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return strArr.count;
}
//每组cell返回的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 0;
}
//每组尾部返回的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 20;
}
//每组头部返回的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 300;
}else if (section == 2){
return 300;
}
return 0;
}
//是否可以对tableview进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
//是否可以对tableview进行移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
//头部的进行自定义view
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerOne = [[UIView alloc]init];
if (section == 0) {
headerOne.backgroundColor = [UIColor blueColor];
}else if (section == 2){
headerOne.backgroundColor = [UIColor redColor];
}
return headerOne;
}
//尾部进行自定义view
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
//选中该cell进行下一步的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
以上的的方法是经常使用的,方法的使用我随便写了一点。
tableview,uicollectionview这两个都是经常用在布局上面的,下来大家可以多多的练习下
这段时间我们在做一个商场的app,增,删,tableview和collectionview很常用用到,明天我在详细的介绍下自定的tableview和数据的删除,移动,增加,今天仓促的写了一点,希望像以前我刚出来的那些初学者可以有一点帮助。
1:UITableView的初始化
UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.view.bound),CGRectGetHeight(self.view.bounds))];
tableview.dataSouce = self;
tableview.delegete = self;
[self.view addSubView:tableview];
以上就是简单的初始化方法,基本我项目的懒加载里面就是这几句,我们看到我们遵守了协议,tableview协议里面有二个我们必须实现的方法。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return:返回uitableview的行数
}
//此方法用于跟cell赋值和cell的重用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}
这里讲下还有几个经常用的方法
//定义分组的组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return strArr.count;
}
//每组cell返回的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 0;
}
//每组尾部返回的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 20;
}
//每组头部返回的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 300;
}else if (section == 2){
return 300;
}
return 0;
}
//是否可以对tableview进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
//是否可以对tableview进行移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
//头部的进行自定义view
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerOne = [[UIView alloc]init];
if (section == 0) {
headerOne.backgroundColor = [UIColor blueColor];
}else if (section == 2){
headerOne.backgroundColor = [UIColor redColor];
}
return headerOne;
}
//尾部进行自定义view
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
//选中该cell进行下一步的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
以上的的方法是经常使用的,方法的使用我随便写了一点。
tableview,uicollectionview这两个都是经常用在布局上面的,下来大家可以多多的练习下
这段时间我们在做一个商场的app,增,删,tableview和collectionview很常用用到,明天我在详细的介绍下自定的tableview和数据的删除,移动,增加,今天仓促的写了一点,希望像以前我刚出来的那些初学者可以有一点帮助。
相关文章推荐
- UI初级连载九----------UITableView的使用
- xcode 4.3.2 storyboard绘制UITableView为理解storyboard使用和tableview有用
- iOS UITableView下拉刷新上拉加载更多MJRefresh类库(推荐)使用初级剑侠篇(欢迎提建议和分享遇到的问题)
- UITableView的简单使用
- 使用UITableView隐藏的复选功能
- UITableView 基本使用方法总结
- 转载:使用UITableView实现树视图
- 转 UITableView 基本使用方法总结
- UITableView 基本使用方法总结
- UITableView使用大全-表视图合集-持续更新
- 第一、UITableView的使用大全
- iphone UITableView及UIWebView的使用
- iPhone开发入门4 - 创建一个UITable View程序,不使用Interface Builder
- iphone UITableView及UIWebView的使用
- UITableView 基本使用方法
- iPhone:UITableVIew 的一些理解
- UITableView 基本使用方法总结
- 使用UIAlertView和UITableView来创建一个有用的UIAlertTableView
- 导航控制器Nav和UITableView的使用(转载)
- UITableView总结:基本使用方法介绍