Comment créer une base très UITableView par programmation dynamique avec la hauteur de la cellule et de la mise en page automatique?

Note: je sais qu'il y a quelques exemples sur les mêmes lignes. Je suis à la recherche pour la plupart des solution de base avec le minimum d'installation requis.

J'ai essayé d'en créer un moi-même, et je sais que je suis loin..

    #import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property(strong, nonatomic) UITableView *tableView;
@property(strong, nonatomic) NSMutableArray *dataArray;
@property (strong, nonatomic) UITableViewCell *customCell;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
[self.tableView setShowsVerticalScrollIndicator:NO];
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:self.tableView];
self.dataArray = [@[@"For the past 33 years, I have looked in the mirror every morning and asked myself: 'If today were the last day of my life, would I want to do what I am about to do today?' And whenever the answer has been 'No' for too many days in a row, I know I need to change something. -Steve Jobs",
@"Be a yardstick of quality. Some people aren't used to an environment where excellence is expected. - Steve Jobs",
@"Innovation distinguishes between a leader and a follower. -Steve Jobs"] mutableCopy];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"CustomCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:249.0/255 green:237.0/255 blue:224.0/255 alpha:1.0];
int dataIndex = (int) indexPath.row % [self.dataArray count];
cell.textLabel.text = self.dataArray[dataIndex];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *views = @{@"label":cell.textLabel};
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|"
options:0
metrics:nil
views:views];
[cell.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]|"
options: 0
metrics:nil
views:views];
[cell.contentView addConstraints:constraints];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//Calculate a height based on a cell
if(!self.customCell) {
self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
}
//Configure the cell
int dataIndex = (int) indexPath.row % [self.dataArray count];
self.customCell.textLabel.text = self.dataArray[dataIndex];
//auto layout
NSDictionary *views = @{@"label":self.customCell.textLabel};
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|"
options:0
metrics:nil
views:views];
[self.customCell.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]|"
options: 0
metrics:nil
views:views];
[self.customCell.contentView addConstraints:constraints];
//Layout the cell
[self.customCell layoutIfNeeded];
//Get the height for the cell
CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
//Padding of 1 point (cell separator)
CGFloat separatorHeight = 1;
return height + separatorHeight;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 140;
}
@end
  • check this -> appcoda.com/self-sizing-cells
  • mais que l'on se fait à l'aide de storyboard pas? Je suis à la recherche d'un totalement par programmation.
InformationsquelleAutor Avnish Gaur | 2014-10-14