您的位置:首页 > 移动开发 > IOS开发

IOS 设置TableViewCell背景透明

2015-08-17 20:56 375 查看

刚在网上看了下有些前辈关于UITableViewCell背景透明的设置方法,之后在自己的工程里面设置了下没有成功,自己找了下原因。

1.UItableViewController在实例化的时候创建了三个图层一个背景层,一个TableView层,一个cell层。如果在设置背景层上背景图片,cell想要让背景透明并且是以背景层的背景图片为背景的话,那么就需要将中间层(TableView层)的背景色设置成透明的。具体代码如下

1.在TableViewController中设置背景图片

UIImageView *bgImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.height)];

    bgImgView.image = [UIImage imageNamed:@"bg-pubthree"];

    UIView * viewTemp=[[UIView alloc] initWithFrame:self.view.frame];

    UITableView * tableTemp=self.tableView;

    self.view=viewTemp;

    [self.view addSubview:bgImgView];

    [self.view addSubview:tableTemp];

2.将TableView的BackgroundColor设置成透明

UIView * view = [[UIView alloc]initWithFrame:CGRectZero];

    self.tableView.backgroundColor = [UIColor clearColor];

3.设置Cell的BackgroundColor为透明

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

{

    static NSString * cellID = @"cell";

    

    FGMessageCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    

    if (cell == nil) {

        cell = [[FGMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }

    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 44)];

    cell.backgroundColor = [UIColor clearColor];

    

    

    

    return cell;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息