1

I am creating dynamic cell as mentioned in image. How can I make dynamic cell in table view.

code

self.arrayContainer = [[NSMutableArray alloc]initWithObjects:
                           [NSDictionary dictionaryWithObjectsAndKeys:
                            @"H.Rackham",@"name",
                            @"20/7/1994",@"date",
                            @"Lorem ipsum dolor sit amet, qui ex blandit posidonium efficiantur, sed te veri bonorum. Per id iriure utroque docendi. Stet patrioque incorrupte ea nam. Prima molestie accommodare nam in, inermis omnesque vix cu.                            Est posse suavitate eu, ei justo labitur necessitatibus has, no sea agam liber. Deseruisse elaboraret eu ius. Est verterem sententiae an. Prompta elaboraret duo cu, aperiam dolorem has an, ubique nominati comprehensam no pri. In autem scripta concludaturque cum.",@"Description",nil],
                           [NSDictionary dictionaryWithObjectsAndKeys:
                            @"Sophia Rhodes",@"name",
                            @"20/7/1994",@"date",
                            @" Deseruisse elaboraret eu ius. Est verterem sententiae an. Prompta elaboraret duo cu, aperiam dolorem has an, ubique nominati comprehensam no pri. In autem scripta concludaturque cum.",@"Description",
                            nil],
                           nil];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arrayContainer count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:identifier];

    cell.textLabel.text = [[self.arrayContainer objectAtIndex:indexPath.row ]valueForKey:@"name"];
    cell.detailTextLabel.text = [[self.arrayContainer objectAtIndex:indexPath.row ]valueForKey:@"date"];
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

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

Image

enter image description here

I have made simple code demo. now I want this type of functionality.

1)Name and date should be display differently as mentioned in image.

2)According to content cell should be resize.

Thank you

Hardik1344
  • 2,917
  • 2
  • 15
  • 31

1 Answers1

2

You have to use CustomCell.

Add a new file to the project, with the Objective-C class template. Name it DetailViewell and make it a subclass of UITableViewCell.

for your first requirement: Please follow this, this will help you : How to create Custom Cell

For your second requirement:

If you set proper autolayout constraints to label then you have to set only following delegate method to achieve this.

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
   //minimum size of your cell, it should be single line of label if you are not clear min. then return UITableViewAutomaticDimension;    
   return UITableViewAutomaticDimension; 
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19