【问题标题】:set UITableViewCell height programmatically after getting json data获取 json 数据后以编程方式设置 UITableViewCell 高度
【发布时间】:2016-04-25 10:29:22
【问题描述】:

我正在尝试在获取 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];
            }
        });
    });
}

【问题讨论】:

    标签: ios objective-c json uitableview


    【解决方案1】:

    如果您想使用UITableViewAutomaticDimension,请不要在heightForRowAtIndexPath 中返回它。像在viewDidload 中一样,

    tableView.estimatedRowHeight = 100.0
    tableView.rowHeight = UITableViewAutomaticDimension
    

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

    希望这会有所帮助:)

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-12
        • 1970-01-01
        • 1970-01-01
        • 2013-12-14
        • 2013-01-28
        相关资源
        最近更新 更多