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和数据的删除,移动,增加,今天仓促的写了一点,希望像以前我刚出来的那些初学者可以有一点帮助。