1

I have TableViewCell and load data by web service (JSON), Data stored in different-different array and data load in tableview. I want to array data size some time large and some time small. so how will manage label size. I tried may examples but do not make dynamic label. please help, Thank You.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [title count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"tableview cell");
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"event"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [img objectAtIndex:indexPath.row]]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];
    cell.img.image =image;

    cell.lbl_1.text=[NSString stringWithFormat:@"%@",[title objectAtIndex:indexPath.row]];

    cell.lbl_2.text=[NSString stringWithFormat:@"%@",[des objectAtIndex:indexPath.row]];
    cell.lbl_3.text=[NSString stringWithFormat:@"%@",[evnt_ary objectAtIndex:indexPath.row]];

    return  cell;
}
Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
A.Kumawat
  • 103
  • 9

6 Answers6

3

Please check UITableViewAutomaticDimension and use it like:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension

Make sure your constraints are proper.

Sagar Shirbhate
  • 801
  • 7
  • 17
1

You just need to write [yourCell.yourlabel sizeToFit] after assigning text to that label.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    yourCell.yourlabel.text = @"text whatever you want to set";
    [yourCell.yourlabel sizeToFit];
}

Also assign text to yourCell.yourlabel and write [yourCell.yourlabel sizeToFit] in heightForRowAtIndexPath.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    yourCustomCell *yourCellObj = (yourCustomCell *)[self tableView:self.tableViewObj cellForRowAtIndexPath:indexPath];

    yourCellObj.yourlabel.text = @"text whatever you want to set";
    [yourCellObj.yourlabel sizeToFit];

    return (yourCellObj.yourlabel.frame.origin.y + yourCellObj.yourlabel.frame.size.height);
}
iHP
  • 11
  • 3
  • In this code don't use both lines _table_view.estimatedRowHeight = 100.0; // _table_view.rowHeight = UITableViewAutomaticDimension;? – A.Kumawat Jul 15 '16 at 09:36
  • I am not sure, but when i tried estimatedRowHeight sometimes i got wrong output. – iHP Jul 15 '16 at 09:53
  • Make sure you set **yourCellObj.numberOfLines = 0** @A.Kumawat – iHP Jul 15 '16 at 10:16
0

First of all, use custom cell for your tableView. If you use label directly on tableView, it will cause loading problems when you change it's size dynamically.

Then in "cellForRowAtIndexPath", you can change it properties. For example, if "TableCell" is your custom cell then

 TableCell *cell = (NBEServicesCell*)[tableView dequeueReusableCellWithIdentifier:"@yourIdentifier" forIndexPath:indexPath];

    //Assigning each row a number.
[cell.yourLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:14.0]];
cell.yourLabel.numberOfLines = 0;
cell.yourLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.yourLabel sizeToFit];
cell.yourlabel.frame.size.width = newWidth;
cell.yourlabel.frame.size.height = newHeight;
WasimSafdar
  • 1,044
  • 3
  • 16
  • 39
0
CGSize maximumSize = CGSizeMake(width_specified, CGFLOAT_MAX);


CGSize myStringSize = [myString sizeWithFont:myFont 
                           constrainedToSize:maximumSize 
                               lineBreakMode:self.myLabel.lineBreakMode];

You can calculate the height required by string to fit in lable or textview for the given width.

You also need to specify the font for the string. ie. font used in container of the string.

Raj Aggrawal
  • 761
  • 1
  • 6
  • 21
0

You have to get dynamic width & height depending on content (text) of label so you can use following function it will return CGSize you just pass your label parameter.

- (CGSize ) messageSizeExact:(UILabel *) msgLabel
{

    CGRect textRect = [msgLabel.text boundingRectWithSize:(CGSize){280.0, CGFLOAT_MAX}
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{NSFontAttributeName:msgLabel.font}
                                            context:nil];

    return textRect.size;
}
Bhaskar
  • 51
  • 5
0

plz take the outlet of table

@property (strong, nonatomic) IBOutlet UITableView *view_tableView;


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view_tableView.estimatedRowHeight = 50;
    self.view_tableView.rowHeight = UITableViewAutomaticDimension;
    // Do any additional setup after loading the view, typically from a nib.
}


#pragma mark - TableView Delegate And DataSource



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return 1;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    UILabel *label =(UILabel *)[cell viewWithTag:5000];

    label.text=@"Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.2016-07-15 15:22:28.819 Time'nTask[257:14278] Notification settings registered2016-07-15 15:22:28.820 Time'nTask[257:14278] Successfully registered for remote notifications With device Token 68048cc293d695fd3220cf63da3baa685675126ecd72af0547b760ee31571b62";

    return cell;
}

rember set label number of line = 0

enter image description here

balkaran singh
  • 2,754
  • 1
  • 17
  • 32