【问题标题】:Locating the UIView that UITableView scrolls over定位 UITableView 滚动的 UIView
【发布时间】:2010-10-20 10:22:51
【问题描述】:

如果启用滚动,我正在尝试获取 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。

有什么想法吗?谢谢。

【问题讨论】:

    标签: iphone objective-c cocoa-touch uiviewcontroller


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 嘿 Roger,我不希望 blackColor 渗入我的 UITableView。我想要它的正常样式。我只想在表格滚动出边界时查看背景。
        猜你喜欢
        • 2017-05-12
        • 1970-01-01
        • 2017-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-03
        • 2017-12-01
        相关资源
        最近更新 更多