找到UITableView滚动的UIView

问题描述:

我正在尝试获取UITableView滚动的UIView(如果启用了滚动)。通常情况下,背景是白色的,如果您将UITableView从边界中移出,则会看到背景。我试图将此背景设置为blackColor的UIColor。我似乎无法找到合适的标签。我在我的UIViewController中试过以下代码:找到UITableView滚动的UIView

- (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。

有什么想法?谢谢。

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

+0

嘿罗杰,我不想让blackColor渗入我的UITableView。我想要它的正常样式。我只想看看表格滚动出界时的背景。 – Coocoo4Cocoa 2009-04-20 15:30:06

我是能够实现你想要什么,但它看起来像一个解决方法,我(也许有对库中的错误)。

首先,您需要在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; 
} 

你必须设置的内容查看到白色的背景颜色,但当你这样做时,单元格的文本停止出现,所以你需要添加一个UILabel到它的内容视图并自己写文本。

这似乎矫枉过正只是改变背景颜色,但可能有一种防止设置上的UITableViewCell的不是其包含的UITableView不同的背景颜色的错误。

嗯..我在一个类似的问题设定cellForRowAtIndexPath方法中的细胞的背景颜色运行。 解决方法是在回调方法 willDisplayCell中设置背景颜色。

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