获取json数据后,以编程方式设置UITableViewCell高度

时间:2016-04-25 10:29:22

标签: ios objective-c json uitableview

我尝试在获取JSON数据后根据内容调整UITableViewCell的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

我已经添加了这样但是UITableViewCell高度无法根据内容增加。请帮我解决。

- (void) getAllocatedJobs {
    _allTableData = [[NSMutableArray alloc]init];
    [SVProgressHUD show];
    dispatch_queue_t getInit = dispatch_queue_create("getinit",NULL);
    dispatch_async(getInit, ^{
        NSDictionary *jsonResponse = [commClass getJobs:strDriverId paramJobType:@""];
        NSNumber *status = [jsonResponse valueForKey:@"Success"];
        NSString *message = [jsonResponse valueForKey:@"Message"];
        NSArray *dataArray = [jsonResponse valueForKey:@"lstJob"];
        dispatch_async(dispatch_get_main_queue(), ^{
            if([status intValue] == 1) {
                for(int i=0; i<[dataArray count]; i++) {
                    NSDictionary *JobData = [dataArray objectAtIndex:i];
                [_allocatedTable reloadData];

            } else {
                [commClass showAlert:APP_NAME alertMessage:message];
                [SVProgressHUD dismiss];
            }
        });
    });
}

2 个答案:

答案 0 :(得分:1)

如果您想使用UITableViewAutomaticDimension,请不要在heightForRowAtIndexPath中退回。请在viewDidload中执行,

tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension

和自动布局对于UITableViewAutomaticDimension是强制性的,因此请确保您已设置了适当的约束。当需要时成功获取所有json数据时重新加载表数据。

希望这会有所帮助:)

答案 1 :(得分:0)

尝试根据此方法中的内容计算高度: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath