iOS 8 Tableview根據AutoLayout自動調整高度,iostableview高度
iOS 8 Tableview根據AutoLayout自動調整高度,iostableview高度
原創Blog,轉載請註明出處
blog.csdn.net/hello_hwc
前言:在iOS 8之前,如果要讓Tableview根據內容自動調整大小的話,需要動態的去計算每個cell的高度。太尼瑪操蛋了。iOS 8之後,可以根據AutoLayout來自動調整高度了,原理很簡單。
- DataSource中選擇讓iOS自動計算
- 在Cell中,設定能夠讓iOS計算出高度的AutoLayout,注意,這裏一定要是能夠計算出高度的AutoLayout,這和傳統的不一樣。
效果
完整過程
新建一個基於singleview的工程,然後刪除默認Storyboard的ViewController,拖拽一個TableviewController,設置爲inital Controller
往Prototype Cells上拖拽兩個UILabel
如圖
設置cell 的reuse identifier爲cell
爲兩個Label設置屬性
Title
設置tag爲10
Detail
設置tag爲11
爲兩個Label設置AutoLayout
Title
Detail
注意,這裏把title放在左上角,Detail放在左下角。然後添加二者之間的距離恆定爲1,那麼AutoLayout就會自動計算出高度。
新建一個TableviewController,並且講storyboard上的tableviewController設置爲新建的類
設置Tableview的高度爲自動獲取
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
加入存儲數據的數組,並且在初始化裏設定數據
@property (strong,nonatomic)NSArray * titleArray;
@property (strong,nonatomic)NSArray * detailArray;
- (void)viewDidLoad {
[super viewDidLoad];
self.titleArray = @[@"1",@"2",@"3"];
self.detailArray = @[@"shot",@"Aduahguhauhguhaudghuahguhudhauhg",@"dhuahgudhaughuahdughuahguhauhguhdahudhuahughduahguhadguhaduhguadhughduahguahguhadugh"];
}
接下來就是Tablview的常用的,很好理解,這裏不多贅述
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.titleArray.count;
}
-(BOOL)prefersStatusBarHidden{
return true;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UILabel * titleLabel = (UILabel *)[cell viewWithTag:10];
UILabel * contentLabel = (UILabel *)[cell viewWithTag:11];
titleLabel.text = self.titleArray[indexPath.row];
contentLabel.text = self.detailArray[indexPath.row];
contentLabel.numberOfLines = 0;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@
"Cell"
];
cell.textLabel.numberOfLines = 100;
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.text = self.arr[indexPath.row];
return
cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* text = self.arr[indexPath.row];
CGSize constraint = CGSizeMake(375-40, 100);
NSAttributedString* attributedText = [[NSAttributedString alloc]initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize size = rect.size;
CGFloat height = MAX(size.height, 20);
NSLog(@
"%f"
,height);
return
height+10;
}