美文网首页
tableView基本用法于常用属性

tableView基本用法于常用属性

作者: 天地一紫罗 | 来源:发表于2017-03-03 10:52 被阅读0次

一、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];

}

相关文章

网友评论

      本文标题:tableView基本用法于常用属性

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