简体   繁体   English

iPhone-SDK:如何设置View的背景颜色而不是Table?

[英]iPhone-SDK:How to set background color for View and not for the Table?

I have downloaded TableView sample code '4_TableViewCellSubviews' from internet. 我从互联网上下载了TableView示例代码'4_TableViewCellSubviews'。 I am trying to run this program in my way. 我试图以我的方式运行这个程序。 I am trying to set my own background view color as 'lightgraycolor'. 我试图将自己的背景视图颜色设置为'lightgraycolor'。 I just wanted to see the background view as lightgraycolor in this application. 我只想在这个应用程序中将背景视图看作lightgraycolor。 I don't want to make any color for TableView. 我不想为TableView制作任何颜色。 ie i want to give gray color only for the background view, not for the background of table. 即我只想为背景视图提供灰色,而不是为了桌子的背景。 When i provide the code for that as '[self.view setBackgroundColor:[UIColor lightGrayColor]];' 当我提供的代码为'[self.view setBackgroundColor:[UIColor lightGrayColor]];' , it is changing the color for the entire table as well. ,它也改变了整个桌子的颜色。 But i am trying to set Color only for the view which besides on the Table. 但我试图只为表格上的视图设置颜色。 How to set background color only for the background view and not for the TableView. 如何仅为背景视图设置背景颜色,而不为TableView设置背景颜色。 I know that the code which i provide to set the color background view is perfect. 我知道我提供的用于设置彩色背景视图的代码是完美的。 But., Could someone who came across with this problem, provide me suggestions what are the possibilities that we would get to occur with this issue and solution for this? 但是,遇到这个问题的人能否为我提供建议,我们可以通过这个问题和解决方案获得这种可能性?

I have kept the sreenshot at the following location: 我把sreenshot保存在以下位置:

http://www.quickfiles.net/894107 http://www.quickfiles.net/894107

NEED URGENT HELP..WAITING FOR THE SOLUTION BY GIVING YOUR PRECIOUS SUGGESTIONS... 需要紧急帮助......通过提出您的宝贵建议,为解决方案做好准备......

I think most you all have misunderstood my problem. 我想大多数人都误解了我的问题。 I want to have color on the space when we scroll DOWN the Table we see a empty space on the top in the background of tableview. 当我们向下滚动表格时,我希望在空间上有颜色,我们在tableview的背景中看到顶部的空白空间。 For example, download a free news application called, "BusinessWeek". 例如,下载名为“BusinessWeek”的免费新闻应用程序。 You can launch this app in your iPhone device. 您可以在iPhone设备中启动此应用程序。 You can see the home screen and scroll DOWN the table there. 您可以看到主屏幕并向下滚动表格。 You can see the background color as Gray color of the Table. 您可以将背景颜色看作表格的灰色。 I want to do the same way, please let me know what should i do for that? 我想以同样的方式做,请让我知道我该怎么做?

Clave/ 来福/

In this case here, the 'view' and 'tableView' are the same actually, that's the color change doesn't work as you want it to. 在这种情况下,'view'和'tableView'实际上是相同的,那就是颜色变化不能按你的意愿工作。 To convince you of that, add these 2 lines in the 'viewDidLoad' function, and look at what is output in the console when you run your program: 为了说服你,在'viewDidLoad'函数中添加这两行,并在运行程序时查看控制台中输出的内容:

NSLog(@"tableView: %@", self.tableView);
NSLog(@"view: %@", self.view);

Sorry, the earlier answer comment needs to modified as below steps. 对不起,先前的答案评论需要修改如下步骤。 I am writing the solution here because some one else if they come across they can use it. 我在这里写解决方案,因为如果他们遇到其他人,他们可以使用它。

  1. In IB, Create UIView and set the color whatever you want. 在IB中,创建UIView并根据需要设置颜色。 Create Table on top of it. 在它上面创建表格。 Create a TableView variable in your code and connect that with IB Table. 在代码中创建一个TableView变量并将其与IB Table连接。

  2. In viewDidLoad, add like below. 在viewDidLoad中,添加如下。 myTableView.backgroundColor = [UIColor clearColor]; myTableView.backgroundColor = [UIColor clearColor]; myTableView.alpha = 0.9; myTableView.alpha = 0.9; [super viewDidLoad]; [super viewDidLoad];

  3. Add the below code in cellForRowAtIndexPath: 在cellForRowAtIndexPath中添加以下代码:

     UIView *v = [[UIView alloc] initWithFrame:cell.frame]; 

    v.backgroundColor = [UIColor whiteColor]; v.backgroundColor = [UIColor whiteColor]; cell.backgroundView = v; cell.backgroundView = v; [v release]; [v发布]; // If you want accessory type add this below line .. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //如果你想要附件类型,请将此行添加到以下行.. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

  4. Build and Run you application. 构建并运行您的应用程序。
[tableView setBackgroundColor:[UIColor clearColor]];

You should add the tableView as a subview of the MainView. 您应该将tableView添加为MainView的子视图。 Create a ViewController class and programmatically add the tableview methods and delegate. 创建一个ViewController类,并以编程方式添加tableview方法和委托。 At program start, add your tableview as a subView of the Mainview. 在程序启动时,将tableview添加为Mainview的子视图。 In this way UITableView and UIView should be two distinct parts, and you should be able to change the background color properly. 这样UITableView和UIView应该是两个不同的部分,你应该能够正确地改变背景颜色。

My problem was also same whatever the below existing query is talking about 'Gutter' color change: 无论以下现有的查询是关于'Gutter'颜色变化的问题,我的问题也是一样的:

Set UITableViewCell background color on plain style table view? 在普通样式表视图上设置UITableViewCell背景颜色?

Currently i have resolved it by the following code: 目前我通过以下代码解决了它:

I have created a UIView and set the background color as 'Gray'. 我创建了一个UIView并将背景颜色设置为“灰色”。 I created a TableView on top of it and connect with the tableView variable there. 我在它上面创建了一个TableView,并在那里连接tableView变量。

When i run my app, it has shown the Gray color in the background view and also in Table. 当我运行我的应用程序时,它在背景视图和表格中显示了灰色。 I don't want that color in Table, so added the below code as well to remove it. 我不希望表中的颜色,所以添加下面的代码也删除它。

in 'viewDidLoad' 在'viewDidLoad'中

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

UIView *myView = [[UIView alloc] initWithFrame:cell.frame]; UIView * myView = [[UIView alloc] initWithFrame:cell.frame]; myView.backgroundColor = [UIColor whiteColor]; myView.backgroundColor = [UIColor whiteColor]; cell.backgroundView = myView; cell.backgroundView = myView; [myView release]; [myView发布];

cell.contentView.backgroundColor = [UIColor whiteColor]; cell.contentView.backgroundColor = [UIColor whiteColor];

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

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