因为业务需求,需要给第一个Row添加一条背景view
在tableview的代理方法willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath中实现
代码:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
for (UIView * subView in tableView.subviews) {
if([subView viewWithTag:202308161821])
{
[subView removeFromSuperview];
}
}
UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0,100, KscreenWidth, 25)];
bgView.backgroundColor = [UIColor colorWithHexString:@“#195CF3”];
bgView.tag = 202308161821;
[tableView addSubview:bgView];
[tableView sendSubviewToBack:bgView];
}
}
判断删除是为了避免创建多个view,只要存在一个即可 sendSubviewToBack这句很重要