tableView基本用法于常用属性

一、tableView的属性

1.取消cell的分割线

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

2.取消tableView右侧的滚动条

tableView.showsVerticalScrollIndicator = NO;

3.当tableview数据较少时,动态隐藏tableView的底部线条

tableView.tableFooterView = [[UIView alloc]init];

4.设置tableView的偏移量

[myTableView setContentOffset:CGPointMake(0, 100)    animated:YES];

5.隐藏tableView的footerView

tableView.sectionFooterHeight = 0;

6.tableView选中时反选

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

{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

8.在tableView索引中显示搜索按钮

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];

[arr addObject:@"{search}"];//等价于[arr addObject:UITableViewIndexSearch]; return arr;

}

二、Cell的属性

1.设置单元格选中时的背景色

系统默认的颜色设置

//无色

cell.selectionStyle = UITableViewCellSelectionStyleNone;

//蓝色

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

//灰色

cell.selectionStyle = UITableViewCellSelectionStyleGray;

方法一、

UIImageView *imageView = [UIImageView alloc]init];

imageView.backgroundColor = [UIColor clearColor];

cell.selectedBackgroundView = imageView;

方法二、

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];

UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

bgview.opaque = YES;

bgview.backgroundColor = [UIColor orangeColor];

[cell setBackgroundView:bgview];

2.设置单元格默认背景色

通过属性设置

cell.contentView.backgroundColor = [UIColor redColor];

通过方法设置

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

cell.backgroundColor = [UIColor redColor];

}

3.取消单元格选中时背景色

cell.textLabel.highlightedTextColor = [UIColor redColor];

4.调整单元格之间的距离

- (void)setFrame:(CGRect)frame

{

// tableView边框的宽度

#define kTableBorderWidth 5

// 更改x、宽度

frame.origin.x = kTableBorderWidth;

frame.size.width -= kTableBorderWidth * 2;

// 更改顶部间距、每个cell之间的间距

frame.origin.y += kTableTopBorderWidth;

frame.size.height -= kTableViewCellMargin;

[super setFrame:frame];

}

5.单元格的属性

cell.accessoryType =UITableViewCellAccessoryNone;//cell没有任何的样式

cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素;

cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button;

cell.accessoryType =UITableViewCellAccessoryCheckmark;//cell右边的形状是对号;

cell.selectionStyle =UITableViewCellSelectionStyleNone;//cell选中状态的样式,为枚举类型

6.tableView的cell之间的分割线长度改变方法

self.settingTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

self.settingTableView.separatorInset=UIEdgeInsetsMake(0, 40, 0, 20);

7.隐藏UITableView多余的分割线

- (void)setExtraCellLineHidden: (UITableView *)tableView

{

UIView *view =[ [UIView alloc]init];

view.backgroundColor = [UIColor clearColor];

[tableView setTableFooterView:view];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容