【问题标题】:unwanted white space "under" tableView subview when tableView scrolled to its content limits当 uitableView 滚动到其内容限制时,不需要的空白“在”uitableView 子视图下
【发布时间】:2023-03-25 21:15:02
【问题描述】:

我有一个 UITableViewCell 作为自定义视图控制器中的子视图。它工作得很好,只是当它滚动到其 contentSize 限制的顶部或底部时,它“继续前进”并在其后面留下一些空白区域。这尤其令人恼火,因为我确实有一个 UIView 覆盖了 tableView 后面的整个屏幕,并且该视图设置为非白色。我还添加了一个子视图,它恰好位于具有相同背景颜色的 tableview 之下,再次尝试阻止白色。我还将 UIApplication Window 背景颜色设置为非白色。这些都不起作用。

我会认为即使我的 TableView 在其原始框架周围反弹,“暴露”视图也应该与底层视图匹配,而不是白色。如何修复我的 tableView 以使其保留所有滚动属性,但在滚动结束时反弹时不会显示白色?

这是效果的屏幕截图。白色出现在 tableViewHeader 和占据屏幕顶部的 UISCrollView 下方。当我将 tableView 一直滚动到一个极端时,就会出现这种情况。如果我一直滚动到另一端,则空白会出现在 tableView 的底部而不是顶部。

这是相关的代码,我觉得很普通:

@interface JudgeViewController () <UITextFieldDelegate, UITextViewDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate, UITableViewDataSource, UITableViewDelegate,  UIViewControllerRestoration, UIScrollViewDelegate>
@property (nonatomic) UITableView *tableView;
@end

设置tableViewCells的函数

#pragma mark - tableview appearance and actions

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StatsViewController *svc  = [[StatsViewController alloc] init];
    svc.user = self.object.answerUser[indexPath.row];
    svc.fromJudge = YES;

    [self.navigationController pushViewController:svc animated:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.object.answerArray count];
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    UILabel *label = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell ==nil)
    {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
        label = [[UILabel alloc] initWithFrame:CGRectZero];
        [label setLineBreakMode: UILineBreakModeWordWrap];
        [label setMinimumFontSize:SMALL_FONT_SIZE];
        [label setNumberOfLines:0];
        [label setFont:[UIFont systemFontOfSize:SMALL_FONT_SIZE]];
        [label setTag:1];

        // [[label layer] setBorderWidth:2.0f];


        [[cell contentView] addSubview:label];

    }


    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    CGFloat height = [[UIScreen mainScreen] bounds].size.height;


    NSString *text = self.object.answerArray[indexPath.row];
    CGSize constraint = CGSizeMake(.8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, 200000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:SMALL_FONT_SIZE] constrainedToSize:constraint];

    if(!label)
        label = (UILabel *)[cell viewWithTag:1];

    [label setText:text];
    [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, .8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, MAX(size.height, 44.0f))];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(.85*width, label.frame.size.height/2-2*CELL_CONTENT_MARGIN, .12*width, 20);
    [button1 setTitle:@"UP" forState:UIControlStateNormal];
    button1.titleLabel.font =[UIFont systemFontOfSize:SMALLEST_FONT_SIZE];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(upVoteA:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button1];
    [cell.contentView bringSubviewToFront:button1];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.frame = CGRectMake(.85*width, label.frame.size.height/2+2*CELL_CONTENT_MARGIN, .12*width, 20);
    [button2 setTitle:@"DOWN" forState:UIControlStateNormal];
    button2.titleLabel.font =[UIFont systemFontOfSize:SMALLEST_FONT_SIZE];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(downVoteA:) forControlEvents:UIControlEventTouchUpInside];

    CGFloat moduloResult = indexPath.row % 2;
    if(moduloResult>0)
    {
       cell.backgroundColor = [UIColor colorWithRed:1 green:0.647 blue:0 alpha:.6];
    }
    else
    {
        cell.backgroundColor = [UIColor colorWithRed:1 green:0.647 blue:0 alpha:.4];
    }
    cell.opaque = NO;
    cell.alpha = 0.2;

    [cell.contentView addSubview:button2];
    [cell.contentView bringSubviewToFront:button2];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

-(void)keyboardToJudge
{

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row < [self.object.answerArray count])
    {
    NSString *text = self.object.answerArray[indexPath.row];
    CGSize constraint = CGSizeMake(.8*CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN)*2, 200000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE-2.0f] constrainedToSize:constraint];
    CGFloat height = MAX(size.height, 44.0f);
    return height + (CELL_CONTENT_MARGIN)*2;
    }
    else
    {
        return 200.0f;
    }

}

设置布局的函数:

-(void)viewDidLoad
{
...among other things setting up top scroll view (top part of view with gray background and orange text)...
   if(self.navigationController.navigationBar.frame.size.height>0)
    {
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 5, width, height*SCROLL_VIEW_OFFSET)];
    }
    else
    {
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBarHeight+5, width, height*SCROLL_VIEW_OFFSET)];
    }
    self.scroll.backgroundColor = BACKGROUND_COLOR;
    self.scroll.contentSize =CGSizeMake(width, .5*height);
    self.scroll.delegate = self;
    self.scroll.contentInset = UIEdgeInsetsMake(30, 0, 30, 0);
    [self.scroll setShowsHorizontalScrollIndicator:NO];



...adding buttons to self.scroll...

    [self.view addSubview:self.scroll];

....


    self.tableView = [[UITableView alloc] initWithFrame:frame];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height*.1)];
    self.tableView.tableFooterView.backgroundColor = BACKGROUND_COLOR;


    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height*.1)];
    self.tableView.tableHeaderView.backgroundColor = BACKGROUND_COLOR;

....tableView hidden state is changed to yes in another function if row count is zero but not usually...
    self.tableView.hidden = NO; 
    [self.view addSubview:self.tableView];
...
}

最后,我也叫:

[self.tableView reloadData];

从网络服务器重新加载数据后,根据结果将 tableView 设置为隐藏或不隐藏(如果有结果要显示,则不隐藏)。那应该是触及 tableView 子视图的每一行代码。

【问题讨论】:

  • 如果你能发布相关的截图和代码,那将有很大帮助。
  • @pawan 感谢您的评论。我认为代码很简单很长,所以我跳过了它,但现在我把它和屏幕截图一起粘贴了。

标签: ios objective-c uitableview


【解决方案1】:

添加到您的viewDidLoad

[self.tableView setBackgroundColor:BACKGROUND_COLOR];

设置tableView的背景色就可以了

【讨论】:

  • 感谢您的建议。我刚试过这个,但空白仍然存在。
  • OOps,我最初的建议很接近,但是关闭...设置 UITableView 的背景,你会没事的。我已经编辑了我的答案
  • 在此文件中包含的头文件中定义。我尝试使用 [UIColor redColor] 并且可以在顶部滚动视图后面看到它,但在 tableView 后面看不到。空白处还在。但是你让我觉得可能 tableView.backgroundColor 必须与单元格颜色和页眉/页脚颜色分开设置,这就是诀窍。谢谢。
  • 我们同时找到了!如果您想发布答案,我将很乐意为您服务。再次感谢。 NM,我看到你编辑了你的答案,所以我会检查它。
【解决方案2】:

改变这一行:

self.scroll.contentInset = UIEdgeInsetsMake(30, 0, 30, 0);

到这里:

self.scroll.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

您正在使用该行向tableview 添加页脚。下图来自 IOS 开发库

【讨论】:

  • 感谢您的评论,但这也没有任何效果。我把它放在上面是为了管理视图覆盖,上面有一个 navBar,下面有一个 tabBar。此外,tableView 的顶部和底部都会出现空白,这取决于我滚动的方向。
  • 事实上,我只是完全删除了该行 - 它是处理覆盖问题的剩余部分,但没有达到我想要的效果。
【解决方案3】:

在尺寸检查器中将表格视图估计值设置为 0(取消选中自动)

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    相关资源
    最近更新 更多