2

如果启用了滚动,我正在尝试获取 UITableView 滚动的 UIView。通常,背景是白色的,如果你将 UITableView 推出它的边界,你会看到一个背景。我正在尝试将此背景设置为 blackColor 的 UIColor。我似乎找不到合适的标签。我在 UIViewController 中尝试了以下代码:

- (void)loadView 
{
    [super loadView];
    UITableView *aTableView = 
        [[[UITableView alloc] 
            initWithFrame:self.view.bounds
            style:UITableViewStylePlain] autorelease];

    [aTableView setScrollEnabled:YES];
    [self.view setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:aTableView];
    self.tableView = aTableView;
}  

颜色仍然保持白色。似乎我打错了 UIView。

任何想法?谢谢。

4

3 回答 3

0

我没有解决方案,但我有一个建议。您是否尝试过使 UITableView 的 backgroundColor 透明并将超级视图的颜色设置为白色?

于 2009-04-19T18:52:30.627 回答
0

我能够实现你想要的,但它对我来说似乎是一种解决方法(也许库上有一个错误)。

首先,您需要在 UITableView 中将 backgroundColor 属性设置为黑色。

然后,当您创建一个新的 UITableViewCell 时,您需要执行以下操作(此代码在 UITableView 的数据源上):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";

    // Try to retrieve from the table view a now-unused cell with the given identifier
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    // If no cell is available, create a new one using the given identifier
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }
    cell.contentView.backgroundColor = [UIColor whiteColor];

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 60, 20)] autorelease];
    label.text = @"test";
    label.textColor = [UIColor blackColor];
    [cell.contentView addSubview:label];
    cell.textColor = [UIColor blackColor];

    return cell;
}

您必须将 contentView 的背景颜色设置为白色,但是当您这样做时,单元格的文本会停止出现,因此您需要在其内容视图中添加一个 UILabel 并自己编写文本。

仅仅改变背景颜色似乎有点过头了,但可能有一个错误会阻止在 UITableViewCell 上设置与包含 UITableView 不同的背景颜色。

于 2009-04-20T22:46:13.177 回答
0

嗯..我在 cellForRowAtIndexPath 方法中设置单元格的背景颜色时遇到了类似的问题。解决方法是在回调方法 willDisplayCell 中设置背景颜色。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
   cell.backgroundColor = (indexPath.row % 2 == 0) ? DARK_BACKGROUND : LIGHT_BACKGROUND;
}
于 2010-01-28T02:17:12.190 回答