我创建了一个带有静态单元格的UITableView,它将显示从Parse加载的酒店信息。这些单元格具有右侧细节样式。 detailText对我来说是未知的,所以它可能很长,这意味着单元格的高度不明。
我尝试使用NSMutableDictionary来解决这个问题,以存储detailLabel的允许宽度(这也是动态的,因为左侧标签因为被本地化而改变宽度),计算detailText的高度并重新加载UITableView的数据。这不太好。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"displaying cell: %@", cell);
NSLog(@"detailwidth : %f", cell.detailTextLabel.frame.size.width);
if (indexPath.section != 0 && ![[cellRefresh objectForKey:indexPath] boolValue]) {
[self performSelector:@selector(customRefreshForCells:) withObject:cell afterDelay:0.5];
[cellRefresh setObject:[NSNumber numberWithBool:YES] forKey:indexPath];
}
}
- (void)customRefreshForCells:(UITableViewCell *)cell
{
NSLog(@"customRefreshForCells - detailwidth : %f", cell.detailTextLabel.frame.size.width);
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithFloat:cell.detailTextLabel.frame.size.width], [NSNumber numberWithFloat:cell.detailTextLabel.frame.size.height], nil];
NSLog(@"setting objects: %@, %@", array, indexPath);
[cellDict setObject:array forKey:indexPath];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (reloadedData == YES && cellDict.count) {
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:16];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
if (indexPath.section == 1) {
if (indexPath.row == 0) label.text = _nameLabel.text;
if (indexPath.row == 1) label.text = _cityLabel.text;
if (indexPath.row == 2) label.text = _locationLabel.text;
if (indexPath.row == 3) label.text = _addressLabel.text;
if (indexPath.row == 4) label.text = _phoneLabel.text;
if (indexPath.row == 5) label.text = _emailLabel.text;
if (indexPath.row == 6) label.text = _siteLabel.text;
} else if (indexPath.section == 2) {
if (indexPath.row == 0) label.text = _breakfastLabel.text;
if (indexPath.row == 1) label.text = _poolLabel.text;
if (indexPath.row == 2) label.text = _callRoomLabel.text;
if (indexPath.row == 3) label.text = _callReceptionLabel.text;
if (indexPath.row == 4) label.text = _internetLabel.text;
if (indexPath.row == 5) label.text = _remarksLabel.text;
}
label.frame = CGRectMake(0, 0, [[[cellDict objectForKey:indexPath] firstObject] floatValue], 0);
[label sizeToFit];
label.frame = CGRectMake(0, 0, [[[cellDict objectForKey:indexPath] firstObject] floatValue], label.frame.size.height);
if (label.frame.size.height + 20 > 44) { // 44 is the default
return label.frame.size.height + 20;
} else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
} else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
我试图为细胞创建一个自定义笔尖,但由于是静态的,无法使其工作。从来没有装过它。
我必须支持iOS7,这很愚蠢。 iOS8有一些很酷的新功能来解决这个问题:)