首先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];