簡體   English   中英

如何在iOS中創建具有動態表格高度的動態表格單元格

[英]How to create dynamic tableview cell with dynamic tableview height in iOS

我想根據內容增加tableview單元格和tableview的高度。 假設tableview包含2條記錄,並且其第一個單元格高度為100,第二個單元格高度為25,則tableview高度應為100 + 25 = 125。 如何實現此功能?

提前致謝。

你絕對可以做到

  1. 首先,確保您對像元的約束subView必須設置為從上到下,以便計算像元所需的高度。

  2. 確保您的委托設置如下

     -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } 
  3. 設置tableView的高度約束,並輸出該約束。

  4. 在要動態調整tableView大小的類中添加以下方法。

     - (void)adjustHeightOfTableview { CGFloat height = self.tableView.contentSize.height; //CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y; /* Here you have to take care of two things, if there is only tableView on the screen then you have to see is your tableView going below screen using maxHeight and your screen height, Or you can add your tableView inside scrollView so that your tableView can increase its height as much it requires based on the number of cell (with different height based on content) it has to display. */ // now set the height constraint accordingly self.constraintHeightTableView.constant = height; //If you want to increase tableView height with animation you can do that as below. [UIView animateWithDuration:0.5 animations:^{ [self.view layoutIfNeeded]; }]; } 
  5. 准備好表的數據源后,請調用此方法,並以

     dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; //In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me) [self performSelector:@selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3]; }); 

如果您運行的是iOS 8+,則可以使用:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80 // your desired or expected height

屬性。

  1. 為了使它生效,您不應在heightForRowAtIndexpath中設置任何高度
  2. 您應該設置單元格約束,即對單元格內存在的元素的約束,因此設置的約束足以使tableviewcell在運行時計算其高度

快速解決方案3

步驟1.設置頂部,底部,前導和尾隨約束(不要將其設置為恆定高度,但從現在開始,請設置恆定的高度約束)。 現在,我們將根據單元格的數量及其高度動態更改此高度。

步驟2.將具有高度限制的出口拖到類中,然后將此函數復制到類中的任何位置。

    func adjustHeightOfTableview() {
    let height: CGFloat = tableview.contentSize.height
    self.outLetOfTheHeightConstraint.constant = height

}

第3步。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    DispatchQueue.main.async {
            self.tableview.reloadData()
            self.perform(#selector(self.adjustHeightOfTableview))
        }
}
self.tableView.frame = CGRectMake(self.tableView.origin.x, self.tableView.origin.y, self.tableView.frame.size.width, 125);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM