Ios 行高未知的静态UITableViewCells

Ios 行高未知的静态UITableViewCells,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我已经创建了一个带有静态单元格的UITableView,它将显示从Parse加载的酒店信息。这些单元格具有样式正确的细节。我不知道细节文本,所以它可能相当长,这意味着单元格的高度未知 我尝试使用NSMutableDictionary来存储detailLabel的允许宽度(这也是动态的,因为左侧标签因本地化而改变宽度),计算detailText的高度并重新加载UITableView的数据。这并不漂亮 - (void)tableView:(UITableView *)tableView willDi

我已经创建了一个带有静态单元格的UITableView,它将显示从Parse加载的酒店信息。这些单元格具有样式正确的细节。我不知道细节文本,所以它可能相当长,这意味着单元格的高度未知

我尝试使用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有一些很酷的新特性来解决这个问题:)

Ah,使用iOS8很容易。。。我会再看看你的问题,看看我是否能想出一个解决iOS7的办法。谢谢你,伙计,我已经花了整整两天的时间来解决这个问题了。去克雷克雷。顺便说一句,我正在使用自动布局。最简单的解决方案。。。告诉PM(或任何人)你不需要支持iOS 7。现在看80%的iOS8:D他想要支持iOS7的唯一原因是他有一台iphone4s,不想更新,因为“这会严重减慢我的iPhone速度,我在某处读到过……”。不管怎样,他都会为此付出代价:)我感觉到你的痛苦。