Wenn Sie für jede Zeile unterschiedliche Höhe einstellen möchten, sollten Sie diese Methode aus UIViewDelegate Protokoll verwenden:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Dies ist ein Beispiel für die Verwendung es, Code für 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
Dies ist MyTableCellTableViewCell.h
#import <UIKit/UIKit.h>
@interface MyTableCellTableViewCell : UITableViewCell
@property UILabel* lbContent;
@end
Und ist dies 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
Hoffe, dass es Ihnen helfen kann.
Mögliche Duplikat [Mit Auto Layout in UITableView für dynamische Zellenlayouts und variable Zeilenhöhen] (http://stackoverflow.com/questions/18746929/using-auto-layout-inuitableview-for-dynamic-cell-layouts-variable-row-heights) –