UITableView基础设置

作者: MakeThatChange | 来源:发表于2016-05-27 19:39 被阅读130次

1.UITableView懒加创建

- (UITableView *)ta{

if (_ta == nil) {

//_ta = [UITableView new];默认方式创建plain

//Group风格会有头和脚,而设置make.top是从头开始到self.top

_ta = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];//带风格地创建

_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)设置cell

//创建几个组,默认值为1

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

return 1;

}

//每个组有几行

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

return 10;

}

//cell的创建

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

static NSString *identifier = @"iphone";

//使用回内存重用的方式创建cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

#warning cell是有风格的,在创建的时候

//带有风格(共4种)地创建cell,并标记重用标志

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

//附加视图

cell.accessoryType = 1;

//被点击后的选中/高亮颜色

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 = @"详细信息";

return cell;

}

2)设置头部和尾部title

//每个分区头部显示的题目

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return @"头部";

}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

return @"尾部";

}

3)设置cell,头部,尾部高度

//自定义行高默认是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)添加索引

//添加右侧分区索引值- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{

return self.arr;

}

//特殊指定点击分区索引值之后的跳转

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

return index;

}

相关文章

网友评论

    本文标题:UITableView基础设置

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