iOS 8 Tableview根據AutoLayout自動調整高度,iostableview高度

iOS 8 Tableview根據AutoLayout自動調整高度,iostableview高度


熱度3  評論 179
www.BkJia.Com  網友分享於:  2015-05-09 09:05:23     瀏覽數17410次
<iframe id="cproIframe_u1983349_4" width="250" height="250" src="http://pos.baidu.com/acom?adn=3&at=231&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=17&ch=0&col=zh-cn&conBW=0&conOP=1&cpa=1&dai=4&dis=0&ltr=http%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DNcK1K-GN_gXRq5UXIZ5dibCv4ZT43I-FBCFNIkC6TPH6QwKdOmOjuxV-5BfrFMoRZSf4vIGBF0FVmgzg8n_2M_%26wd%3Dios%25E8%2587%25AA%25E5%258A%25A8%25E8%25AE%25A1%25E7%25AE%2597%26issp%3D1%26f%3D8%26ie%3Dutf-8%26tn%3Dbaiduhome_pg%26bs%3Dcocochina&ltu=http%3A%2F%2Fwww.bkjia.com%2FAndroidjc%2F996451.html&lu_161=0&lunum=6&n=35088068_cpr&pcs=1151x660&pis=10000x10000&ps=578x101&psr=1280x800&pss=1151x608&qn=107d3beca5a667df&rad=&rsi0=250&rsi1=250&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000ff&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=tabcloud_skin_3&stid=5&td_id=1983349&titFF=%E5%AE%8B%E4%BD%93&titFS=12&titTA=left&tn=text_default_250_250&tpr=1434129908338&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1983349&ti=iOS%208%20Tableview%E6%A0%B9%E6%8D%AEAutoLayout%E8%87%AA%E5%8A%A8%E8%B0%83%E6%95%B4%E9%AB%98%E5%BA%A6%EF%BC%8Ciostableview%E9%AB%98%E5%BA%A6_Android%E6%95%99%E7%A8%8B%20%7C&tt=1434129908326.346.489.489" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true"></iframe>
<iframe id="cproIframe_u1044360_5" width="300" height="250" src="http://pos.baidu.com/acom?adn=3&at=231&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=17&ch=0&col=zh-cn&conBW=0&conOP=1&cpa=1&dai=5&dis=0&ltr=http%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DNcK1K-GN_gXRq5UXIZ5dibCv4ZT43I-FBCFNIkC6TPH6QwKdOmOjuxV-5BfrFMoRZSf4vIGBF0FVmgzg8n_2M_%26wd%3Dios%25E8%2587%25AA%25E5%258A%25A8%25E8%25AE%25A1%25E7%25AE%2597%26issp%3D1%26f%3D8%26ie%3Dutf-8%26tn%3Dbaiduhome_pg%26bs%3Dcocochina&ltu=http%3A%2F%2Fwww.bkjia.com%2FAndroidjc%2F996451.html&lu_161=6&lunum=6&n=35088068_cpr&pcs=1151x660&pis=10000x10000&ps=578x369&psr=1280x800&pss=1151x844&qn=1e14c41292a6939c&rad=&rsi0=300&rsi1=250&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000ff&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=tabcloud_skin_3&stid=5&td_id=1044360&titFF=%E5%AE%8B%E4%BD%93&titFS=12&titTA=left&tn=text_default_300_250&tpr=1434129908338&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1044360&ti=iOS%208%20Tableview%E6%A0%B9%E6%8D%AEAutoLayout%E8%87%AA%E5%8A%A8%E8%B0%83%E6%95%B4%E9%AB%98%E5%BA%A6%EF%BC%8Ciostableview%E9%AB%98%E5%BA%A6_Android%E6%95%99%E7%A8%8B%20%7C&tt=1434129908326.494.571.571" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true"></iframe>

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;
     
     
}
發佈了40 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章