2014-10-27 61 views
2

我有一个自定义的UITableViewCell卡片样式动态高度,它的空间是通过应用程序不断变化。我正在使用故事板,并在UINavigationBar下面有UIToolbar。最后两个单元格之间奇怪的UITableViewCell差距

enter image description here

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (!_listOfMessages) { 
     return 0; 
    } 
    else if (_unreadMessages && [self.segmentControl selectedSegmentIndex] == 0) 
     return _unreadMessages.count; 
    else 
     return _listOfMessages.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    WCFeedItem *item; 

    if ([self.segmentControl selectedSegmentIndex] == 0) { 
     item = _unreadMessages[indexPath.row]; 
    } 
    else { 
     item = _listOfMessages[indexPath.row]; 
    } 

    RKCardInboxCell *cell = (RKCardInboxCell *)[tableView dequeueReusableCellWithIdentifier:@"RKCardInboxCell"]; 

    // Configure the cell... 
    if (cell == nil) { 
     cell = [[RKCardInboxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RKCardInboxCell"]; 
    } 
    else { 
     [[[cell.contentView viewWithTag:99] viewWithTag:97] removeFromSuperview]; 
     cell.cardView.backgroundColor = [UIColor whiteColor]; 
    } 

    [cell layoutSubviews]; 

    [cell.profileImage setImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:item.person.avatar_feed]] placeholderImage:[UIImage imageNamed:@"loading"] success: 
    ^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
     cell.profileImage.image = image; 
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
    }]; 

    [cell setItem:item]; 


    cell.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; 
    cell.titleLabel.numberOfLines = 0; 

    CGRect expectedLabelSize = [item.summary boundingRectWithSize:CGSizeMake(268, MAXFLOAT) 
                  options:NSStringDrawingUsesLineFragmentOrigin 
                 attributes:@{NSFontAttributeName:cell.titleLabel.font} 
                  context:nil]; 

    //  NSLog(@"size %@", NSStringFromCGSize(expectedLabelSize.size)); 
    CGRect newFrame = cell.titleLabel.frame; 
    newFrame.size.height = expectedLabelSize.size.height; 
    cell.titleLabel.frame = newFrame; 

    cell.titleLabel.text = item.summary; 
    cell.nameLabel.text = item.person.name; 
    cell.descriptionLabel.text = item.messageTo; 
    [cell.timeButton setTitle:item.date forState:UIControlStateNormal]; 

    if (item.isUnseen) { 
     cell.cardView.backgroundColor = [UIColor colorWithRed:0.94 green:0.97 blue:1 alpha:1]; 

     UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(10,20,10,10)]; 
     circleView.alpha = 0.5; 
     circleView.layer.cornerRadius = 5; 
     circleView.backgroundColor = [UIColor colorWithRed:0 green:0.2 blue:0.4 alpha:1.0]; 
     circleView.tag = 97; 
     CGRect timeLabel = [[cell.contentView viewWithTag:99] viewWithTag:98].frame; 
     circleView.center = CGPointMake((timeLabel.origin.x + timeLabel.size.width) - circleView.frame.size.width, timeLabel.origin.y + timeLabel.size.height + circleView.frame.size.height); 
     [[cell.contentView viewWithTag:99] addSubview:circleView]; 
     //[cell bringSubviewToFront:circleView]; 
    } 


    cell.cardView.frame = CGRectMake(10, 5, 300, 160-8+expectedLabelSize.size.height-5); 

    return cell; 

} 


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

    UIFont *font = [UIFont systemFontOfSize:13]; 

    NSString *aboutText = [_listOfMessages[indexPath.row] summary]; 


    // ios 7 only 
    CGRect boundingRect = [aboutText boundingRectWithSize:CGSizeMake(268, MAXFLOAT) 
                options:NSStringDrawingUsesLineFragmentOrigin 
               attributes:@{NSFontAttributeName:font} 
                context:nil]; 

    CGSize boundingSize = boundingRect.size; 
    // end ios7 only 

// [cardSizeArray addObject:[NSNumber numberWithInt:160-8+boundingSize.height]]; 

    return (160-8+boundingSize.height); 
} 
+1

截图和代码会让这个问题更好。 – CrimsonChris 2014-10-27 16:18:09

+0

我现在要这么做 – SeifScape 2014-10-27 16:24:33

回答

0

是不是因为cardView是300点宽,但heightForRowAtIndexPath使用268点?它看起来有问题的行有文本,可能会包装到两行268,但只有一行300.没有看到RKCardInboxCell布局代码很难说清楚。

顺便说一句,像这样的硬编码宽度将使它很难支持更宽的设备,如iPhone 6和6 Plus。

+0

非常感谢!实际上,这加上我忘了设置字体大小,所以一旦我补充说,它的工作就像一个魅力!另外,我会记住这一点,并在iPhone 6和6 Plus出来之前写下它! – SeifScape 2014-10-27 22:19:42