首先要创建一个工程,命名为TableViewAutoLayout:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tabVi;
@end
//
// ViewController.m
// TableViewAutoLayout
//
// Created by 杨潇 on 15/4/8.
// Copyright (c) 2015年 PaoPaoKeJi. All rights reserved.
//
#import "ViewController.h"
#import "YXAutoLayoutTableViewCell.h"
@interface ViewController ()
{
NSMutableArray * _dataArr;
}
@end
#import "ViewController.m"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dataArr = [[NSMutableArray alloc] initWithObjects:@"你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?",@"你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?",@"你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?",@"你好吗?你好吗?你好吗?你好吗?你好吗?",@"你好吗?你好吗?你好吗?你好吗?",@"你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?",@"你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?",@"你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?你好吗?",
nil];
_tabVi.rowHeight = UITableViewAutomaticDimension;
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{ return 38;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * identifier = @"YXAutoLayoutTableViewCell";
YXAutoLayoutTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (nil == cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"YXAutoLayoutTableViewCell" owner:nil options:nil] firstObject];
}
cell.textLab.text = _dataArr[indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
连heightForRow都不需要实现,简直是爽爆了有木有,感觉离安卓的开发又近了一步
本文介绍了一个使用自动布局的TableView的实现方法。通过设置TableView的rowHeight为UITableViewAutomaticDimension,并配合YXAutoLayoutTableViewCell自定义单元格,实现了不同长度文本自动调整高度的功能。
6142

被折叠的 条评论
为什么被折叠?



