简体   繁体   中英

UITableView in UITableViewCell won't scroll to bottom

I have a UITableViewCell with a UITableView inside using a custom cell with a height of 77. I cannot scroll all the way though the UITableView, the last row is cut off.

I am using this code to create the frame for the table view.

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

_routeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, screenWidth, screenHeight) style:UITableViewStylePlain];

Here is a short video showing my issue:

Does anyone have any suggestions how I can properly scroll through the routes?

It seems there is no space for cells upward, not just cutting off the last cell.

-(void)configureRouteTableView
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    _routeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, screenWidth, screenHeight - 50) style:UITableViewStylePlain];

    _routeTableView.delegate = self;
    _routeTableView.dataSource = self;
    _routeTableView.hidden = YES;
    _routeTableView.separatorColor = [UIColor clearColor];
    [_routeTableView setBackgroundColor:[UIColor clearColor]];
    [self.contentView addSubview:_routeTableView];
}

My personal experience

1) Make sure scroll content size a multiple of the uitableview cell size

2) Make sure your tableview's y-coordinate plus tableview's height does not exceed the screen.

For your case, I think it is number 2, that's causing your trouble. Since,

_routeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, screenWidth, screenHeight) style:UITableViewStylePlain];

50+screenHeight, means some part of your tableview is off the screen. :)

Thanks to suggester for suggesting. I'm not sure why I can't make use of your editing.

As I see you have two options to fix this issue:

1) First adjust the frame of the UITableView:

_routeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, screenWidth, screenHeight - self.navigationController.navigationBar.frame.size.height) style:UITableViewStylePlain];

which means the height of the table view is the height of screen minus height of navigationbar and starts just after navigationbar on y-axis.

2) Give some height to your UITableView's footer. (not recommended)

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 50.0f; }

Also please make sure that this delegate function returns actual cell height:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return yourTableCellHeight;
}

我需要将框线更改为:

_routeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, screenWidth, 90) style:UITableViewStylePlain];

Please set tableview cell height in following method.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    return height;//77

}

Try to use contentInsets of tableview. You could setup it in IB. But disable auto layout first.

I fixed this using Autolayout constraints instead of letting iOS do the (wrong) thing:

If you use a Podfile, install UIView+Autolayout :

pod 'UIView+Autolayout'

include UIView-Autolayout.h

#import "UIView-Autolayout.h"

and use this in your viewDidLoad:

[tableView setTranslatesAutoresizingMaskIntoConstraints:NO];
[tableView pinToSuperviewEdges:JRTViewPinAllEdges inset:0.0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM