我最后一个UITableView单元底部的空白区域在哪里?

时间:2014-10-31 04:37:30

标签: ios objective-c uitableview ios7

  • 我的问题是,这个丑陋的白色空间出现在我UITableView的底部。
  • 获取单元格分隔线的唯一方法(无论它叫什么)是一直滚动到底部并备份,然后它会出现。
  • 但即使它出现,我的最后一个牢房下方仍有一大片白色空间。

这是我的两张图片来说明我的问题。


Image without the cell separator line present


Image with the cell separator line present (I have to scroll to get this)


这是我的代码:

Post *currentPost;

@interface CommentViewController ()

@end

@implementation CommentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];
    [self.leftLabel setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];
    [self.rightLabel setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://kmjt.org/getComments.php"]];
    NSData *data = [NSData dataWithContentsOfURL:url];

    jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    postArray = [[NSMutableArray alloc] init];

    for(int i = 0; i < jsonArray.count; i++)
    {
        NSString *nickname = [[jsonArray objectAtIndex:i] objectForKey:@"nickname"];
        NSString *squeal = [[jsonArray objectAtIndex:i] objectForKey:@"comment"];
        NSString *timeSincePosted = [[jsonArray objectAtIndex:i] objectForKey:@"TIME_TO_SEC(TIMEDIFF(NOW( ), c.timePosted))"];

        [postArray addObject:[[Post alloc] initWithNickname:nickname andSqueal:squeal andTimeSincePosted:timeSincePosted]];
    }

    viewArray = [[NSMutableArray alloc] init];

    UILabel *nicknameLabelMainPost = [[UILabel alloc]initWithFrame:CGRectMake(43, 5, 320-10, 30)];
    nicknameLabelMainPost.backgroundColor = [UIColor clearColor];
    nicknameLabelMainPost.text = currentPost.nickname;
    nicknameLabelMainPost.font = [UIFont boldSystemFontOfSize:20];

    UITextView *textViewMainPost = [[UITextView alloc] initWithFrame:CGRectMake(38, 22, 320-34, 0)];
    textViewMainPost.backgroundColor = [UIColor clearColor];
    textViewMainPost.font = [UIFont systemFontOfSize:15];
    textViewMainPost.text = currentPost.squeal;
    [textViewMainPost sizeToFit];
    [textViewMainPost layoutIfNeeded];
    textViewMainPost.userInteractionEnabled = NO;

    UILabel *timePostedLabelMainPost = [[UILabel alloc]initWithFrame:CGRectMake(43, 50+textViewMainPost.contentSize.height-20, 320-10, 15)];
    timePostedLabelMainPost.backgroundColor = [UIColor clearColor];
    timePostedLabelMainPost.text = [self getTimeSincePostedLabel:currentPost.timeSincePosted];
    timePostedLabelMainPost.textColor = [UIColor colorWithRed:(181/255.0) green:(181/255.0) blue:(189/255.0) alpha:1.0f];
    timePostedLabelMainPost.font = [UIFont boldSystemFontOfSize:15];

    UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 320, 30+textViewMainPost.frame.size.height)]; // 30 is nickname label height, 20 is time posted label height

    [view addSubview:nicknameLabelMainPost];
    [view addSubview:textViewMainPost];
    [view addSubview:timePostedLabelMainPost];

    [viewArray addObject:view];

    for(int i = 0; i < postArray.count; i++)
    {
        Post *postObject;
        postObject = [postArray objectAtIndex:i];

        UILabel *nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 5, 320-65, 30)];
        nicknameLabel.backgroundColor = [UIColor clearColor];
        nicknameLabel.text = postObject.nickname;
        nicknameLabel.font = [UIFont boldSystemFontOfSize:15];

        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(70, 22, 320-65, 0)];
        textView.backgroundColor = [UIColor clearColor];
        textView.font = [UIFont systemFontOfSize:10];
        textView.text = postObject.squeal;
        [textView sizeToFit];
        [textView layoutIfNeeded];
        textView.userInteractionEnabled = NO;

        UILabel *timePostedLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 50+textView.contentSize.height-20, 320-65, 15)];
        timePostedLabel.backgroundColor = [UIColor clearColor];
        timePostedLabel.text = [self getTimeSincePostedLabel:postObject.timeSincePosted];
        timePostedLabel.textColor = [UIColor colorWithRed:(181/255.0) green:(181/255.0) blue:(189/255.0) alpha:1.0f];
        timePostedLabel.font = [UIFont boldSystemFontOfSize:15];

        UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 320, 30+textView.frame.size.height+20-20+2)]; // 30 is nickname label height, 20 is time posted label height

        [view addSubview:nicknameLabel];
        [view addSubview:textView];
        [view addSubview:timePostedLabel];

        [viewArray addObject:view];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%i", viewArray.count); // This is returning 3 (the correct amount of rows)
    return viewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    else
        [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    [cell.contentView addSubview:[viewArray objectAtIndex:indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *view = [viewArray objectAtIndex:indexPath.row];
    return view.frame.size.height+30;
}

- (NSString *)getTimeSincePostedLabel:(NSString *)secondsString
{
    int num_seconds = [secondsString integerValue];

    int days = num_seconds / (60 * 60 * 24);
    num_seconds -= days * (60 * 60 * 24);
    int hours = num_seconds / (60 * 60);
    num_seconds -= hours * (60 * 60);
    int minutes = num_seconds / 60;

    if(days > 0)
        return [NSString stringWithFormat:@"%i d", days];

    else if(hours > 0)
        return [NSString stringWithFormat:@"%i h", hours];

    else if(minutes > 0)
        return [NSString stringWithFormat:@"%i m", minutes];

    return [NSString stringWithFormat:@"%i s", num_seconds];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

我认为你的heightForRowAtIndexPath存在问题,每当你将视图高度提高30时,请尝试使用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *view = [viewArray objectAtIndex:indexPath.row];
    CGFloat cellHeight =  view.frame.size.height;
    return cellHeight+30;
}