原文地址:http://www.cocoachina.com/industry/20140604/8668.html
- UINib *cellNib = [UINib nibWithNibName:@"C1" bundle:nil];
- [self.tableView registerNib:cellNib forCellReuseIdentifier:@"C1"];
- self.tableData = @[@"1\n2\n3\n4\n5\n6", @"123456789012345678901234567890", @"1\n2", @"1\n2\n3", @"1"];
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- // Return the number of rows in the section.
- return self.tableData.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- C1 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C1"];
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- return cell;
- }
- @property (nonatomic, strong) UITableViewCell *prototypeCell;
- self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"C1"];
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C1 *cell = (C1 *)self.prototypeCell;
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
- NSLog(@"h=%f", size.height + 1);
- return 1 + size.height;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C2 *cell = (C2 *)self.prototypeCell;
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
- CGSize textViewSize = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)];
- CGFloat h = size.height + textViewSize.height;
- h = h > 89 ? h : 89; //89是图片显示的最低高度, 见xib
- NSLog(@"h=%f", h);
- return 1 + h;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- C3 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C3"];
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- [cell.t sizeToFit];
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C3 *cell = (C3 *)self.prototypeCell;
- NSString *str = [self.tableData objectAtIndex:indexPath.row];
- cell.t.text = str;
- CGSize s = [str calculateSize:CGSizeMake(cell.t.frame.size.width, FLT_MAX) font:cell.t.font];
- CGFloat defaultHeight = cell.contentView.frame.size.height;
- CGFloat height = s.height > defaultHeight ? s.height : defaultHeight;
- NSLog(@"h=%f", height);
- return 1 + height;
- }
- - (CGSize)calculateSize:(CGSize)size font:(UIFont *)font {
- CGSize expectedLabelSize = CGSizeZero;
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
- NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
- expectedLabelSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
- }
- else {
- expectedLabelSize = [self sizeWithFont:font
- constrainedToSize:size
- lineBreakMode:NSLineBreakByWordWrapping];
- }
- return CGSizeMake(ceil(expectedLabelSize.width), ceil(expectedLabelSize.height));
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- C4 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C4"];
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- [cell.t sizeToFit];
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C4 *cell = (C4 *)self.prototypeCell;
- NSString *str = [self.tableData objectAtIndex:indexPath.row];
- cell.t.text = str;
- CGSize s = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)];
- CGFloat defaultHeight = cell.contentView.frame.size.height;
- CGFloat height = s.height > defaultHeight ? s.height : defaultHeight;
- return 1 + height;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- C5 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C5"];
- cell.t.text = @"123";
- cell.t.delegate = self;
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C5 *cell = (C5 *)self.prototypeCell;
- cell.t.text = self.updatedStr;
- CGSize s = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)];
- CGFloat defaultHeight = cell.contentView.frame.size.height;
- CGFloat height = s.height > defaultHeight ? s.height : defaultHeight;
- return 1 + height;
- }
- #pragma mark - UITextViewDelegate
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- if ([text isEqualToString:@"\n"]) {
- NSLog(@"h=%f", textView.contentSize.height);
- }
- return YES;
- }
- - (void)textViewDidChange:(UITextView *)textView {
- self.updatedStr = textView.text;
- [self.tableView beginUpdates];
- [self.tableView endUpdates];
- }