简体   繁体   English

如何设置UITableView的背景(tableview样式为“Grouped”)才能使用图像?

[英]How can I set the background of UITableView (the tableview style is “Grouped”) to use an image?

如何设置UITableView的背景(tableview样式为“Grouped”)才能使用图像?

In newer versions of the SDK, you'll need to set tableView.backgroundView if you want it to be transparent, try something like this: 在较新版本的SDK中,如果您希望它是透明的,您需要设置tableView.backgroundView ,尝试这样的事情:

tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;

We need to do something about that plain background. 我们需要对这个简单的背景做点什么。 We're going to use a PNG image and display it behind the UITableView. 我们将使用PNG图像并将其显示在UITableView后面。

  1. Prepare a PNG image. 准备PNG图像。 It should be either 320x460 (if you have the status bar visible in your app) or 320x480 (if you hide it). 它应该是320x460(如果您的应用程序中显示状态栏)或320x480(如果您隐藏它)。
  2. Drag it into XCode into the Resources folder and add to your project 将其拖入XCode并进入Resources文件夹并添加到您的项目中
  3. Load the NIB file containing your UITableView into Interface Builder 将包含UITableView的NIB文件加载到Interface Builder中
  4. Open the library (Tools> Library), switch to the Media tab, and drag the image to the View, create a new UIImageView. 打开库(工具>库),切换到“媒体”选项卡,然后将图像拖到视图中,创建一个新的UIImageView。
  5. Use the inspector to move and resize the image so it's at X=0, Y=0, Width=320, Height=480 使用检查器移动图像并调整其大小,使其位于X = 0,Y = 0,宽度= 320,高度= 480
  6. Put the UIImageView behind the UITableView (Layout > Send to Back) 将UIImageView放在UITableView后面(Layout> Send to Back)
  7. Save, Build and Go! 保存,构建和开始!

Disappointingly, you won't be able to see your background. 令人失望的是,你将无法看到你的背景。 The UITableView's background is blocking us from seeing the UIImageView. UITableView的背景阻止我们看到UIImageView。 There are three changes you need to make: 您需要进行三项更改:

  1. In the Attributes Inspector, make sure the UITableView's "opaque" checkbox is unchecked! 在Attributes Inspector中,确保未选中UITableView的“opaque”复选框!
  2. Set the UITableView's background color to transparent: 将UITableView的背景颜色设置为透明:

    tableView.backgroundColor = [UIColor clearColor]; tableView.backgroundColor = [UIColor clearColor];

I hope this helps and solves your problem. 我希望这有助于解决您的问题。 It has worked for me and I have yet to find a more elegant way to display a background image for a UITableView. 它对我有用,我还没有找到一种更优雅的方式来显示UITableView的背景图像。

The advantage of my solution, in comparison with setting a background image directly on the UITableView, is that you can indent the table's content. 与直接在UITableView上设置背景图像相比,我的解决方案的优势在于您可以缩进表格的内容。 I often wanted to do this to just show two or three table cells at the bottom of the screen. 我经常想这样做只是在屏幕底部显示两个或三个表格单元格。

[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"whatever.png"]]];

tableView.backgroundView = nil; tableView.backgroundView = nil; is enough. 足够。 No need to set background color as Clear Color. 无需将背景颜色设置为“清除颜色”。

In UI Builder the Background color has an "Other" choice. 在UI Builder中,背景颜色具有“其他”选项。 This brings up a color picker. 这会产生一个颜色选择器。 The color picker has an opacity setting. 颜色选择器具有不透明度设置。 If you set the Opacity of the COLOR to 0 it works, can't speak to performance. 如果将“颜色”的“不透明度”设置为0,则无法正常工作。

One way would be to make the table view transparent (set the view's background to 0% opacity) and place a UIImageView behind the UITableView. 一种方法是使表视图透明(将视图的背景设置为0%不透明度)并将UIImageView放在UITableView后面。 Remember that transparent tables and table cells will not perform as well as opaque ones. 请记住,透明表和表格单元格的表现不如不透明表格。

What I've found is that you have to use a "plain" styled table with a transparent background and then recreate the look of the rounded-corner cells by setting each cell's backgroundView to a UIImageView with a image that simulates the rounded look. 我发现你必须使用具有透明背景的“普通”样式表,然后通过将每个单元格的backgroundView设置为具有模拟圆形外观的图像的UIImageView来重新创建圆角单元格的外观。 This means that the top, bottom, and middle cells need different background images. 这意味着顶部,底部和中间单元需要不同的背景图像。

However, this does not address what happens when the user taps the cell and it goes "highlighted" - it will look squared off then. 然而,这并没有解决当用户点击单元格时它会“突出显示”时会发生什么 - 它会看起来平方。 You can get around this by setting the highlighted image for your faked tablecell background image. 您可以通过为伪造的表格单元格背景图像设置突出显示的图像来解决此问题。 You will also want to create your own disclosure accessory view (ImageView) with a white highlighted version. 您还需要使用白色突出显示版本创建自己的公开附件视图(ImageView)。 Then you can create a cell like this one I'm using (below). 然后你可以创建一个我正在使用的单元格(如下)。 After I alloc one of these cells I then set the backgroundView and accessoryView to my UIImageViews. 在我分配其中一个单元格之后,我将backgroundView和accessoryView设置为我的UIImageViews。

#import "ClearBackRoundedTableCell.h"


@implementation ClearBackRoundedTableCell

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier 
{
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
    }
    return self;
}


-  (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if( [[self.accessoryView class] isSubclassOfClass:[UIImageView class]] )
        ((UIImageView *)self.accessoryView).highlighted = highlighted;

    if( [[self.backgroundView class] isSubclassOfClass:[UIImageView class]] )
        ((UIImageView *)self.backgroundView).highlighted = highlighted;

    self.textLabel.highlighted = highlighted;
}

@end

One note if you go this route: the cells in a grouped table are typically 300 px wide (in portrait mode) but your plain table here would need to be 302 wide to allow for the grey line on each side of the table, which is normally outside of the "content" of the table cell. 如果你走这条路线就会注意到:分组表格中的单元格通常是300像素宽(纵向模式),但是这里的普通表格需要为302宽,以允许表格两边的灰线,这是通常在表格单元格的“内容”之外。

After spending a while with color picker, I found out that you need to specify opaque background not for the table view cell xib, but for the Table View where the cells will be located, which is another xib. 花了一段时间用颜色选择器后,我发现你需要指定不透明的背景而不是表视图单元格 xib,而是指定单元格所在的表视图 ,这是另一个xib。 From what I have seen, table view cell background attributes have no visual effect. 从我所看到的,表视图单元格背景属性没有视觉效果。

try this one 试试这个

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;

It worked for me in grouped tableview. 它在分组tableview中对我有用。

使UITableview背景为清晰的颜色。

Programmatically you can do it like this if your image is added into your resources: 如果将图像添加到资源中,则可以通过编程方式执行此操作:

self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
UIImage *backroundImage = [UIImage imageNamed:@"my_backround"];
UIImageView *backroundImageView = [[UIImageView alloc] initWithImage:backroundImage];

Else you can do it in Interface Builder with this style : 另外,您可以使用以下样式在Interface Builder中执行此操作:

IB tableview与背后的图像

You may need to configure the header files interface from UITableViewController to UIViewController and add <UITableViewDataSource,UITableViewDelegate> ,also don't forget to set the attributes of the tableview to not be opaque and reconnect the tableviews datasource and delegate outlets to the viewcontroller. 您可能需要将头文件界面从UITableViewController配置为UIViewController并添加<UITableViewDataSource,UITableViewDelegate> ,也不要忘记将tableview的属性设置为不透明,并将tableviews数据源和委托出口重新连接到viewcontroller。

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

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