我有一个自定义的UITableViewCell卡样式动态高度,它通过应用程序的空间是不变的。我正在使用故事板,我在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);
}
答案 0 :(得分:0)
是因为cardView
是300点宽,heightForRowAtIndexPath
使用268点吗?它看起来有问题的行有文本可能会在268处换行到两行但在300处只有一行。如果没有看到RKCardInboxCell
的布局代码,那么很难确定。
另外,像这样的硬编码宽度将难以支持iPhone 6和6 Plus等更广泛的设备。