gpt4 book ai didi

ios - 自动调整(动态高度)表格 View 单元格的问题

转载 作者:搜寻专家 更新时间:2023-10-31 23:10:39 25 4
gpt4 key购买 nike

我在 UIViewController 中有一个 UITableView 作为 View 的一部分。我正在尝试根据文本的长度自动调整表格单元格的大小。它不设置 textview 的文本,并且单元格以 idCellTextView 单元格的标准高度结束。我四处搜索并尝试在单元格内的 TextView 中相对于内容 View 和代码在自动布局中使用固定,如下所示。 :

tableView.estimatedRowHeight = 70
tableView.rowHeight = UITableViewAutomaticDimension

与问题相关的 View Controller 的其余部分如下所示:

class ExperimentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
var expId: String = ""
let experimentDescription: String = ""
var cellDescriptors: NSMutableArray!
var visibleRowsPerSection = [[Int]]()


let notificationCenter = NotificationCenter.default

override func viewDidLoad() {
super.viewDidLoad()


//TABLE CELLS
loadCellDescriptors()
tableView.tableFooterView = UIView()
tableView.layoutMargins = UIEdgeInsets.zero
tableView.separatorInset = UIEdgeInsets.zero
tableView.delegate = self
tableView.dataSource = self
tableView.isHidden = true
tableView.estimatedRowHeight = 70
tableView.rowHeight = UITableViewAutomaticDimension
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellIdentifier")
tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "idCellNormal")
tableView.register(UINib(nibName: "HintCell", bundle: nil), forCellReuseIdentifier: "idCellTextView")


//ADD VIEWS
baseView.addSubview(backgroundImage)
baseView.addSubview(foregroundImage)
baseView.addSubview(nameLabel)
baseView.addSubview(segmentedController)
baseView.addSubview(descriptionText)
baseView.addSubview(hint)
baseView.addSubview(tableView)

}



/*
Get property list for the different cells we want to have in the hint table
*/
func loadCellDescriptors() {
if let path = Bundle.main.path(forResource: "CellDescriptor", ofType: "plist") {
cellDescriptors = NSMutableArray(contentsOfFile: path)
getIndicesOfVisibleRows()
tableView.reloadData()
}
}

/*
Returns number of rows in a given section
*/
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return visibleRowsPerSection[section].count
}

/*
Returns number of sections in table
*/
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
/*if cellDescriptors != nil {
return cellDescriptors.count
}
else {
return 0
}*/
print(items.count)
return 3
}

/*
Sets up the cell contents
*/
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: currentCellDescriptor["cellIdentifier"] as! String , for: indexPath) as! CustomCell

if currentCellDescriptor["cellIdentifier"] as! String == "idCellNormal" {
print("1")
if currentCellDescriptor["primaryTitle"] != nil {
cell.labelOfRow.text = "Hintet er"
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
}
} else if currentCellDescriptor["cellIdentifier"] as! String == "idCellTextView" {

cell.textView.delegate = self
cell.textView.isEditable = false
cell.textView.isSelectable = false
cell.textView.isScrollEnabled = true
cell.textView.text = "orem Ipsum er rett og slett dummytekst fra og for trykkeindustrien. Lorem Ipsum har vært bransjens standard for dummytekst helt siden 1500-tallet, da en ukjent boktrykker stokket en mengde bokstaver for å lage et prøveeksemplar av en bok. Lorem Ipsum har tålt tidens tann usedvanlig godt, og har i tillegg til å bestå gjennom fem århundrer også tålt spranget over til elektronisk typografi uten vesentlige endringer. Lorem Ipsum ble gjort allment kjent i 1960-årene ved lanseringen av Letraset-ark med avsnitt fra Lorem Ipsum, og senere med sideombrekkingsprogrammet Aldus PageMaker som tok i bruk nettopp Lorem Ipsum for dummytekst.orem Ipsum er rett og slett dummytekst fra og f"
}
cell.layoutMargins = UIEdgeInsets.zero

return cell
}

/*
Handles row selections
Finds the index of the cell that was selected, checks if it is/can be expanded and animates in the additional rows
*/
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexOfTappedRow = visibleRowsPerSection[indexPath.section][indexPath.row]
let cellDescriptor = cellDescriptors[indexPath.section] as! NSMutableArray
let cell = (cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfTappedRow]
var shouldExpandAndShowSubRows: Bool = false

//Checks if cell is expandable, if it is we check if cell isn't already expanded. If it isn't we can expand the cell
if (cell as AnyObject)["isExpandable"] as! Bool == true {
shouldExpandAndShowSubRows = false
if (cell as AnyObject)["isExpanded"] as! Bool == false {
shouldExpandAndShowSubRows = true
}
}

(cell as AnyObject).setValue(shouldExpandAndShowSubRows, forKey: "isExpanded")

//Makes the cells inside the specific section visible
for i in (indexOfTappedRow + 1)...(indexOfTappedRow + ((cell as AnyObject)["additionalRows"] as! Int)) {
(cellDescriptor[i] as AnyObject).setValue(shouldExpandAndShowSubRows, forKey: "isVisible")
}

//Update the overview of the currently visible rows
getIndicesOfVisibleRows()
//Reload the sections to see the changes
tableView.reloadSections(NSIndexSet(index: indexPath.section) as IndexSet, with: UITableViewRowAnimation.fade)
}

/*
Get overview of the rows that are currently visible
*/
func getIndicesOfVisibleRows() {
visibleRowsPerSection.removeAll()

for currentSectionCells in cellDescriptors.objectEnumerator().allObjects as! [[[String:AnyObject]]] {
var visibleRows = [Int]()

for row in 0...((currentSectionCells).count - 1) {
if currentSectionCells[row]["isVisible"] as! Bool == true {
visibleRows.append(row)
}
}

visibleRowsPerSection.append(visibleRows)
}
}

/*
Get cell descriptor from file for specific cell
*/
func getCellDescriptorForIndexPath(_ indexPath: IndexPath) -> [String: AnyObject] {
let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row]
let cellDescriptor = (cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfVisibleRow] as! [String: AnyObject]
return cellDescriptor
}
}

最佳答案

试试这个:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

Here Example

关于ios - 自动调整(动态高度)表格 View 单元格的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42297199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com