1.UITableView懶加創(chuàng)建
- (UITableView *)ta{
if (_ta == nil) {
//_ta = [UITableView new];默認(rèn)方式創(chuàng)建plain
//Group風(fēng)格會(huì)有頭和腳,而設(shè)置make.top是從頭開始到self.top
_ta = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];//帶風(fēng)格地創(chuàng)建
_ta.delegate = self;
_ta.dataSource = self;
[self.view? addSubview:_ta];
[_ta mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.equalTo(0);
make.top.equalTo(20);
}];
//去除多余部分cell
_tableView.tableFooterView = [UIView new];
}
return _ta;
}
2.UITableView的代理類方法
1)設(shè)置cell
//創(chuàng)建幾個(gè)組,默認(rèn)值為1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//每個(gè)組有幾行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
//cell的創(chuàng)建
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"iphone";
//使用回內(nèi)存重用的方式創(chuàng)建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
#warning cell是有風(fēng)格的,在創(chuàng)建的時(shí)候
//帶有風(fēng)格(共4種)地創(chuàng)建cell,并標(biāo)記重用標(biāo)志
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
//附加視圖
cell.accessoryType = 1;
//被點(diǎn)擊后的選中/高亮顏色
cell.selectionStyle = 1;
//自定義選中視圖
UIView *bgView = [UIView new];
bgView.backgroundColor = [UIColor greenColor];
cell.selectedBackgroundView = bgView;
//自定義輔助視圖
UISwitch *swi = [UISwitch new];
cell.accessoryView = swi;
//自定義背景視圖
UIView *bV = [UIView new];
bV.backgroundColor = [UIColor lightGrayColor];
cell.backgroundView = bV;
//自定義頭像視圖
cell.imageView.image = [UIImage imageNamed:@"anno"];
}
NSString *sectionTitle = [self.arr objectAtIndex:indexPath.section];
cell.textLabel.text = [NSString stringWithFormat:@"%@%ld",sectionTitle, indexPath.row];
cell.detailTextLabel.text = @"詳細(xì)信息";
return cell;
}
2)設(shè)置頭部和尾部title
//每個(gè)分區(qū)頭部顯示的題目
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"頭部";
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"尾部";
}
3)設(shè)置cell,頭部,尾部高度
//自定義行高默認(rèn)是44像素
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
//自定義組頭高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
4)添加索引
//添加右側(cè)分區(qū)索引值- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.arr;
}
//特殊指定點(diǎn)擊分區(qū)索引值之后的跳轉(zhuǎn)
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return index;
}