#import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOutlet UILabel *testLabel; - (void)parseResult:(NSString *)result; @end #import "TestCell.h" @implementation TestCell - (void)awakeFromNib { } - (void)parseResult:(NSString *)result { _testLabel.text= result; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
#import "ViewController.h" #import "TestCell.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { NSArray *dataArray; } @property (weak, nonatomic) IBOutlet UITableView *testTableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; dataArray = @[@"1234\n56789012\n34567\n]\n890\n12341\n2\n3\n4\n5\n6", @"12345\n67890\n1234\n56789\n01234\n567890", @"1\n2", @"1\n2\n3", @"1\n"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath]; [cell parseResult:dataArray[indexPath.row]]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell"]; [cell parseResult:dataArray[indexPath.row]]; CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; NSLog(@"第%ld行的高度=%f",indexPath.row ,size.height + 1); return 1 + size.height; } @end