我有一个自定义的UITableViewCell Card样式动态高度,它的空间在应用程序中是恒定的。我正在使用情节提要,并且UINavigationBar下面有UIToolbar。
- (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);
}
最佳答案
是因为cardView
的宽度为300点,而heightForRowAtIndexPath
使用的是268点?看起来问题所在的行中的文本可能会在268处换成两行,但在300处只有一行。如果看不到RKCardInboxCell
的布局代码,就很难确定。
顺便说一句,像这样的硬编码宽度将使它难以支持iPhone 6和6 Plus等更宽的设备。