Iphone 查找UITableView滚动的UIView

Iphone 查找UITableView滚动的UIView,iphone,objective-c,cocoa-touch,uiviewcontroller,Iphone,Objective C,Cocoa Touch,Uiviewcontroller,如果启用了滚动,我正在尝试获取UITableView滚动的UIView。通常,背景是白色的,如果将UITableView推出其边界,您将看到背景。我正在尝试将此背景设置为黑色。我似乎找不到合适的标签。我在UIViewController中尝试了以下代码: - (void)loadView { [super loadView]; UITableView *aTableView = [[[UITableView alloc] initWi

如果启用了滚动,我正在尝试获取UITableView滚动的UIView。通常,背景是白色的,如果将UITableView推出其边界,您将看到背景。我正在尝试将此背景设置为黑色。我似乎找不到合适的标签。我在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;
}  
颜色仍然保持白色。看来我打错了方向


有什么想法吗?谢谢。

我没有解决方案,但我有一个建议。您是否尝试过将UITableView的背景色设置为透明,并将superview的颜色设置为白色?

我没有解决方案,但我有一个建议。您是否尝试过将UITableView的背景色设置为透明,并将superview的颜色设置为白色?

我能够实现您的要求,但对我来说,这似乎是一种解决方法,可能库中存在错误

首先,需要在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不同的背景颜色。

我能够实现您想要的,但对我来说,这似乎是一个解决方法。库上可能有一个错误

首先,需要在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添加到其内容视图中,并自己编写文本


仅仅更改背景颜色似乎有些过分,但可能有一个错误阻止在UITableView单元格上设置与其包含的UITableView不同的背景颜色。

Hmm。。在cellForRowAtIndexPath方法中设置单元格的背景色时遇到了类似的问题。 解决方案是在回调方法中设置背景色 威尔·凯勒

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
   cell.backgroundColor = (indexPath.row % 2 == 0) ? DARK_BACKGROUND : LIGHT_BACKGROUND;
}

嗯。。在cellForRowAtIndexPath方法中设置单元格的背景色时遇到了类似的问题。 解决方案是在回调方法中设置背景色 威尔·凯勒

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
   cell.backgroundColor = (indexPath.row % 2 == 0) ? DARK_BACKGROUND : LIGHT_BACKGROUND;
}

嘿,罗杰,我不想让黑色渗入我的视线。我想要它的普通款式。我只想在表格滚动超出其边界时看到背景。嘿,罗杰,我不想黑色渗透到我的UITableView中。我想要它的普通款式。我只想在表格被滚动到边界之外时看到背景。