-1

I have to Create a Module in which I have to display data of an user. for that I have two web service. First web service gives the Number of rows with url for each row to load data. for example first web service return json like

{
    "0": {
       "name":"Personal Info",
       "url":"http://www.google.com/profile"
    },
    "1": {
       "name":"Contact Info",
       "url":"http://www.google.com/contact"
    }
}  

Now I have to load each url data on Table View cell and Cell height would be dynamic. Could you please give me advise

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Virendra Gupta Feb 13 '17 at 10:04

3 Answers3

1

What you need to do is take a label in your cell and set its number of lines to be 0 from attribute inspector. Set its constraint from top, leading, trailing and bottom (with respect to cell). Don't fix its height. And after you fetch and set the data to the label.text add following lines after you set data in your cellForRowAtIndexPath:-

[_tableView beginUpdates];
[_tableView endUpdates];

This will set the fetched info on label (that will grow its size according to the text) and also your cell will resize itself accordingly. Also add this method:-

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return  UITableViewAutomaticDimension
}

And in your viewDidLoad() method give some estimated height as follows:-

self.tableView.estimatedRowHeight = 140
Ishika
  • 2,187
  • 1
  • 17
  • 30
0

use basic type uitableview cell then its label set line number to 0 and call below method for dynamic cell height.

  override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
       return UITableViewAutomaticDimension
        }
Chirag Patel
  • 1,453
  • 1
  • 11
  • 21
0

If you want to set different height for each row, you should use this method from UIViewDelegate Protocol:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

This a sample for use it, code for ViewController.m:

#import "ViewController.h"
#import "MyTableCellTableViewCell.h"

@interface ViewController ()

@property NSMutableArray* dataArray;
@property UIFont* cellTextFont;

@end

NSString* cellID = @"MyTableViewCell";

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

_cellTextFont = [UIFont systemFontOfSize:15];

_dataArray = [[NSMutableArray alloc] initWithObjects:
              @"Hello world!",
              @"Hello world!Hello world!Hello world!",
              @"Hello world!Hello world!Hello world!Hello world!Hello world!",
              @"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!",
              @"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!",
              nil];

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.dataSource = self;
self.tableView.delegate = self;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//Set different row height by Odd and even
//    if (0 == indexPath.row % 2) {
//        return 30;
//    }
//    else
//        return 50;

//Set different row height by content text's height
NSString* contentStr = [_dataArray objectAtIndex:indexPath.row];
float textWidth = self.tableView.frame.size.width;
NSDictionary *attributes = @{NSFontAttributeName: _cellTextFont};
CGRect labelSize = [contentStr boundingRectWithSize:CGSizeMake(textWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
return labelSize.size.height;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableCellTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (nil == cell) {
    cell = [[MyTableCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.lbContent.font = _cellTextFont;
}
cell.lbContent.text = [_dataArray objectAtIndex:indexPath.row];
return cell;
}

@end

This is MyTableCellTableViewCell.h

#import <UIKit/UIKit.h>

@interface MyTableCellTableViewCell : UITableViewCell

@property UILabel* lbContent;

@end

And, this is MyTableCellTableViewCell.m:

@interface MyTableCellTableViewCell()

@property UIView* separation;

@end

@implementation MyTableCellTableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

_lbContent = [[UILabel alloc] init];
_lbContent.numberOfLines = INT_MAX;
[self addSubview:_lbContent];

_separation = [[UIView alloc] init];
_separation.backgroundColor = [UIColor blackColor];
[self addSubview:_separation];

return self;
}

-(void)layoutSubviews{
float separationHeight = 1;
_lbContent.frame = self.bounds;
_separation.frame = CGRectMake(0, self.bounds.size.height-separationHeight, self.bounds.size.width, separationHeight);
}

@end

Hope it can help you.

Alanc Liu
  • 1,294
  • 10
  • 15