简体   繁体   English

iOS 7 UItableview单元格背景视图

[英]iOS 7 UItableview cell background view

I am using a image view as tableview cell background view. 我使用图像视图作为tableview单元格背景视图。 When I am compiling my source in xcode 4.x, it works fine ie, In both iOS 6.x and 7.0 it is working fine. 当我在xcode 4.x中编译我的源代码时,它工作正常,即,在iOS 6.x和7.0中,它工作正常。 But when I compile my source in xcode 5.0, the background image view is not appearing iOS 7. 但是当我在xcode 5.0中编译我的源代码时,背景图像视图没有出现在iOS 7中。

Any idea, why it is not working? 任何想法,为什么它不起作用? is there any restrictions on uitableview cell background view in iOS 7? 在iOS 7中对uitableview单元格背景视图有任何限制吗?

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:accountProfileTypeCell];
    UIImageView *cellBgView =[[UIImageView alloc]init];
    [cellBgView setImage:[UIImage imageNamed:@"cellBgView.png"]];
    [cell setBackgroundView:cellBgView];
}

尝试添加:cell.backgroundColor = [UIColor clearColor];

You must set the background in willDisplayCell. 您必须在willDisplayCell中设置背景。 The below code is same as yours only thing is that move it in willDisplay method 下面的代码与你的相同,只是在willDisplay方法中移动它

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

    if(cell.backgroundView==nil){
        UIImageView *cellBgView =[[UIImageView alloc]init];
        [cellBgView setImage:[UIImage imageNamed:@"cellBgView.png"]];
        [cell setBackgroundView:cellBgView];
    }
}

Try this. 尝试这个。 It's work for me 这对我有用

 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:accountProfileTypeCell];
UIView *cellBgView =[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [cell frame].size.width, [cell frame].size.height)];
[cellBgView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cellBgView.png"]]];
[cell setBackgroundView:cellBgView];

The only thing missing is setting frame to cellBgView. 唯一缺少的是将帧设置为cellBgView。 Setting image to it will not resize imageView. 将图像设置为不会调整imageView的大小。 However if you initialize imageView with initWithImage: , it will resize imageView to fit the image. 但是,如果使用initWithImage:初始化imageView,它将调整imageView的大小以适合图像。

You just need to apply autosizing in your imageview. 您只需在imageview中应用自动调整大小。

If your imageview is applied to an entire screen. 如果您的imageview应用于整个屏幕。 Assign all margins to it. 为其分配所有边距。

Checkout the image 检查图像

设置自动调整视图以使其与iOS6和iOS7兼容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM