Tableview是iOS开发中最为普遍的应用组件,可以吐槽的说没有一个应用不调用tableview。所以你得熟练的掌握它,在此我总结了一些常用的设置供大家参考:
1.cellForRowAtIndexPath方法调用(用于设置cell样式)
//设置cell具体内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PersonInfoCell";
//初始化cell并指定类型
TLDPersonInfoCell *cell = (TLDPersonInfoCell *)[self.personTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *cellnNibs = [[NSBundle mainBundle] loadNibNamed:@"TLDPersonInfoCell" owner:self options:nil];
if (cellnNibs) {
cell = (TLDPersonInfoCell *)[cellnNibs firstObject];
}
}
// [self.personTableView setSeparatorInset:(UIEdgeInsetsMake(0, 0, 0, 0))];//设置tableview里的cell自带分割线充满屏幕
//自定义并添加cell分割线样式
UIView *Cellbtmline=[[UIView alloc]initWithFrame:CGRectMake(0, 39.5, kMAIN_SCREEN_WIDTH, 0.5)];
Cellbtmline.backgroundColor=newLineColor;
UIView *Celltopline=[[UIView alloc]initWithFrame:CGRectMake(0,0, kMAIN_SCREEN_WIDTH, 0.5)];
Celltopline.backgroundColor=newLineColor;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell自带右边箭头
// _personTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//将cell设置为不需要分割线
//对应各自的分区
switch (indexPath.section) {
case 0:
{
}
break;
default :
break;
}
return cell;
2.其他几个常用方法:
//分区间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
return 5.0;
}
//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
//各分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return 1;
case 1:
return 5;
default:
return 2;
}
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==2&&indexPath.row==1) {
return 60;
}
if(indexPath.section==0)
{
return 70;
}else
{
return 40;
}
}
3.设置tableview底部多余cell不显示
(1).tableview为Plain模式:
_myAccountTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
(2).tableview为Grouped模式:
去掉顶部多余的间距:
CGRect frame=CGRectMake(0, 0, 0, CGFLOAT_MIN);
_myAccountTableView.tableHeaderView=[[UIView alloc]initWithFrame:frame];
4.效果如下:
模拟器和真机运行的cell效果不太一样,建议真机运行。
本文详细介绍了 iOS 开发中 TableView 的常见配置与使用方法,包括 cell 样式设置、分区间距、行高及数量调整等,并提供了消除底部多余 cell 和调整顶部间距的具体实现。

1494

被折叠的 条评论
为什么被折叠?



