简体   繁体   English

iPhone,我可以有一个自定义的UITableView单元格和表格的背景图像吗?

[英]iPhone, can I have a custom UITableView cell and a background image for the table?

I'd like to have the contents of my table to scroll around on top of my background image. 我想让表格的内容在背景图片上滚动。

or I don't mind if the cells can take chunks of the background image, so that the background remains behind the cells but scrolls with the cells. 或者我不介意这些单元格是否可以拍摄背景图像的一部分,以使背景保留在这些单元格后面,但随着这些单元格滚动。

Is this possible? 这可能吗? if so how ? 如果可以的话如何?

I've tried adding an image view and setting my cells and table background colour to clear. 我尝试添加图像视图并设置单元格和表格背景颜色以清除。

Here's my code. 这是我的代码。

http://www.kidsmaskfactory.com/code/testback.zip can you have a look ? 您可以看看http://www.kidsmaskfactory.com/code/testback.zip吗?

As stated earlier, problem is with your code. 如前所述,问题出在您的代码上。 From the code-sample ( http://kidsmaskfactory.com/code/testback.zip ) you've given, you need to do 2 modifications: 根据您提供的代码示例( http://kidsmaskfactory.com/code/testback.zip ),您需要进行2处修改:

1) Change your parent class of RootViewController from UITableViewController to UIViewController. 1)将RootViewController的父类从UITableViewController更改为UIViewController。 2) Set the view property of File Owner to your view not to your table view. 2)将文件所有者的视图属性设置为您的视图,而不是表视图。

After this re-compile & run your code & it should give you following output. 重新编译并运行代码后,它应该给您以下输出。

替代文字

Try changing the backgroundView of your UITableView to whatever you want - probably a UIImageView with your picture in it. 尝试将UITableView的backgroundView更改为所需的内容-可能是其中包含图片的UIImageView。

This will give you a background image. 这将为您提供背景图像。 Now the question is how can you make the background image scroll with your table. 现在的问题是,如何使背景图像随着桌子滚动。 I'm not sure but there are two posibilities: 我不确定,但有两种可能性:

  1. Since UITableView is a subclass of UIScrollView -- the backroundView will be scrolled with the rest of the table, and you won't need to do anything. 由于UITableView是UIScrollView的子类,因此backroundView将与表的其余部分一起滚动,因此您无需执行任何操作。

  2. The backgroundView is a separate view which is not derived from UIScrollView. backgroundView是一个单独的视图,它不是从UIScrollView派生的。 You will then need to implement the UIScrollViewDelegate to intercept changes in scroll position, and manually apply those changes to your backgroundView. 然后,您将需要实现UIScrollViewDelegate来拦截滚动位置的更改,并将这些更改手动应用于您的backgroundView。

Hi Jules Follow the steps below to set the backgound image to tableview. Jules嗨,请按照以下步骤将背景图像设置为tableview。 This works!! 这有效!! I have tried it. 我试过了

STEP-1 第1步

In your xib put your tableview and allocate it's delegate and datasource fields to File's Owner. 在您的xib中,放置表格视图,并将其委托和数据源字段分配给File的Owner。

STEP-2 第2步

Now Drag a imageView on to your tableview and resize it to size of your View. 现在将imageView拖到您的表视图上,并调整其大小为View的大小。 select the image you want to display in the background using the Inspector. 使用检查器选择要在后台显示的图像。

STEP-3 步骤3

Select the imageView and Go to Menu "Layout -> Send to Back" 选择imageView并转到菜单“布局->发送回”

STEP-4 第四步

Select your tableview and set background color as "clear color" from the Inspector. 选择您的表视图,然后在检查器中将背景色设置为“透明色”。

Hope this solves your problem. 希望这能解决您的问题。

I just downloaded your test project and here is what I think. 我刚刚下载了您的测试项目,这就是我的想法。

You've made your rootviewcontroller in an odd way. 您已经以一种奇怪的方式创建了rootviewcontroller。 It's a specialization of UITableViewController, but you've added your own UITableView as a child view, along with your UIImageView. 这是UITableViewController的特长,但是您已经添加了自己的UITableView作为子视图以及UIImageView。 There is a much easier way. 有一种更简单的方法。

Your RootViewController should simply be regular UITableViewController-derived class, with no changes to the structure in Interface Builder. 您的RootViewController应该只是常规的UITableViewController派生类,而无需在Interface Builder中更改结构。 If you want to define your UIImageView in UInterface Builder then drag a UIImageView into the XIB but not as a child of the UITableView. 如果要在UInterface Builder中定义UIImageView,则将UIImageView拖动到XIB中,而不要作为UITableView的子级。 Make it a top level object. 使它成为顶级对象。 Hook it up to a new IBOutlet you add in code to RootViewController: 将其连接到新的IBOutlet,将其添加到RootViewController中的代码中:

@interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {

    IBOutlet UIImageView* _backgroundImageView;

}

Be sure to set the image in the UIImageView in Interface Builder. 确保在Interface Builder中的UIImageView中设置图像。

In your RootViewController.m file, add the following: 在您的RootViewController.m文件中,添加以下内容:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.backgroundView = _backgroundImageView;
}

I modified your test project and posted it here: http://www.codeveloper.com/TestCustomCellWithViewBackground-TomSwift.zip 我修改了您的测试项目,并将其发布在这里: http : //www.codeveloper.com/TestCustomCellWithViewBackground-TomSwift.zip

step1: change the RootViewController.h to: 步骤1:将RootViewController.h更改为:

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UITableView *reportTableView;

}

@end

step2: 第2步:

In RootViewController.m: 在RootViewController.m中:

add this method: 添加此方法:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    reportTableView.backgroundColor = [UIColor clearColor];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

step3: 第三步:

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

change: 更改:

if (cell == nil) 
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"Cell"
                                                             owner:nil options:nil];

    for (id currentObject in topLevelObjects) 
    {
        if ([currentObject isKindOfClass:[UITableViewCell class]]) 
        {
            cell = ((ReportCell *) currentObject);
            break;
        }
    }

    cell.backgroundColor = [UIColor clearColor];
}

Step4: 第四步:

Go to Nib file 转到Nib文件

-> set the reportTableView outlet to tableview. ->将reportTableView出口设置为tableview。

-> set the view outlet to the view. ->将视图出口设置为视图。

This will solve your problem. 这样可以解决您的问题。

Put an image view behind the table view. 将图像视图放在表格视图后面。 And change the background color to of the UITableView as clear ([UIColor clearColor]). 并将UITableView的背景色更改为clear([UIColor clearColor])。

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

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