不受欢迎的空白区域"" tableView滚动到其内容限制时的tableView子视图

时间:2014-12-03 05:48:23

标签: ios objective-c uitableview

我有一个UITableViewCell作为自定义视图控制器中的子视图。它工作得很好,除了当它滚动到其contentSize限制的顶部或底部时,它会继续“#34;并留下一些暴露在它后面的白色空间。这特别令人恼火,因为我有一个UIView覆盖tableView后面的整个屏幕,并且该视图设置为非白色。我还添加了一个子视图,其中使用相同的背景颜色正好在tableview下面,再次尝试阻止白色。我还将UIApplication Window背景颜色设置为非白色。这些都没有奏效。

我原以为即使我的TableView在其原始帧周围反弹,"暴露"视图应与基础视图匹配,而不是白色。如何修复我的tableView,使其保留所有滚动属性,但在滚动结束时反弹时不会显示白色?

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

unwanted white space screen shot

这是相关的代码,我认为非常香草:

@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子视图的每一行代码。

3 个答案:

答案 0 :(得分:1)

添加到您的viewDidLoad

[self.tableView setBackgroundColor:BACKGROUND_COLOR];

设置tableView的背景颜色,你会没事的

答案 1 :(得分:0)

更改此行:

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

对此:

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

您正在使用该行向tableview添加页脚。以下图像来自IOS开发库 enter image description here

答案 2 :(得分:0)

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