美文网首页
总结UITableViewController属性,方法,使用。

总结UITableViewController属性,方法,使用。

作者: 丶逝水流年 | 来源:发表于2017-01-09 10:06 被阅读239次

首先tableview在iOS里是一个最最常用的一个控件,总结一下,加深印象也是好的

在这里只说到自己曾经使用过的,其他还有很多没有用到的,欢迎大家指正

说明:属性 和方法

详细的属性由于太多了就不在本文写了,本文主要讲解使用方法和注意事项。贴出链接即可TableViewController 属性-方法大全

第一步:自定义cell(由于系统自带的 cell 不能满足很多情况,所以这里直接说一下自定义的)

command + n  新建一个继承自UITableViewCell的类

然后就是往这个自定义的cell里面添加控件,这个看自己需求添加,添加完成之后连线到相应的类里(属性和事件)这里贴上示例

第二步:遵守协议,指定协议,实现代理方法(3步曲)   

先注册自定义好的那个cell

[self.tableView registerNib:[UINib nibWithNibName:@"LTDisciplineCell" bundle:nil] forCellReuseIdentifier:@"LTDisciplineCell1"];//这是注册xib的方式

2.1 遵守协议

  UITableViewDelegate,UITableViewDataSource

2.2  指定协议

 self.tableView.delegate = self;

self.tableView.dataSource = self;

2.3 实现代理方法

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

return 1;

}

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

return 3;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.section == 2) return 310;

if (indexPath.section == 1) return 80;

return 50;

}

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

static NSString *ltDisciplineCell1 = @"LTDisciplineCell1";

static NSString *ltDisciplineCell2 = @"LTDisciplineCell2";

static NSString *ltDisciplineCell3 = @"LTDisciplineCell3";

if (indexPath.section == 0) {

LTDisciplineCell *cell = [tableView dequeueReusableCellWithIdentifier:ltDisciplineCell1];

return cell;

}else if (indexPath.section ==1 ){

LTDisciplineCell2 *cell = [tableView dequeueReusableCellWithIdentifier:ltDisciplineCell2];

return cell;

}else{

LTDisciplineCell3 *cell = [tableView dequeueReusableCellWithIdentifier:ltDisciplineCell3];

return cell;

}

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

DLog(@"--");//选中cell调用这个方法

}

第三步:使用细节

//隐藏cell的分割线

self.tableView.separatorStyle=NO;

//去掉底部多余的表格线

self.tableView.tableFooterView= [[UIViewalloc]initWithFrame:CGRectZero];

/**

设置tableView的背景色

*/

CGRectrect = [[selfview]bounds];

UIImageView*imageView = [[UIImageViewalloc]initWithFrame:rect];

[self.viewsetBackgroundColor:[UIColorclearColor]];

self.tableView.opaque=NO;

self.tableView.backgroundView= imageView;

//让tableView开始的cell往下移一段距离的代码

self.tableView.contentInset=UIEdgeInsetsMake(20,0,0,0);

//如果有很多cell,根据模型的数据来跳转到同一个控制器里,如果是跳转不同的控制器就要一个一个判断

//根据模型数据跳转同一个cell

HomeModel *m = [[HomeModel alloc]init];

m = self.labelData[indexPath.row];

HMDetailController *detailVc = [[HMDetailController alloc] init];

detailVc.cellId = m.c_id;

[self.navigationController pushViewController:detailVc animated:YES];

//cell不同跳转不同控制器

if(index.row == 0){

//执行的方法/跳转的控制器

}

//去掉UItableview headerview黏性(sticky)

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

if (scrollView == self.tableView)

{

CGFloat sectionHeaderHeight = 64; //sectionHeaderHeight

if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {

scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);

} else if (scrollView.contentOffset.y >= sectionHeaderHeight) {

scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);

}}}

//改变tableview样式

//- (instancetype)init{

//    return [self initWithStyle:UITableViewStyleGrouped];

//}

tableview.bounces=NO;//反弹属性关闭,只是一个属性而已

//取消选中  点击之后手放开就选中效果消失再执行下面的代码

[tableView deselectRowAtIndexPath:indexPath animated:YES];

相关文章

网友评论

      本文标题:总结UITableViewController属性,方法,使用。

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