gpt4 book ai didi

ios - 运行时的 Swift UITableViewCell 动态单元格高度

转载 作者:行者123 更新时间:2023-11-29 01:20:04 25 4
gpt4 key购买 nike

关于如何设法完成下面的用例的任何想法。虽然我设法使用 UITableView 做到了这一点,但每次滚动 table view 时我仍然会遇到一些问题。

场景:table view 需要根据选中的项目动态调整单元格的高度。每个项目内部都有不同的选项。选择选项时,它应该能够在项目下显示选项并自动调整高度。

解决方案:我将 UITableViewUITableViewCell 子类化。每次 Options选中/点击时,我都会调用 beginUpdateendUpdate,它们运行良好。

问题:每次用户向下滚动时,选项都不会正确显示。

enter image description here

enter image description here

enter image description here

enter image description here

最佳答案

每次向上或向下滚动时,uitableview 都会调用 cellForRowAtIndexPath 并且 tableView 将重新加载。

因此,在这种情况下,您需要在向上或向下滚动之前追踪选中的单元格,而在滚动之后,您只需将状态(例如高度)替换为所需的单元格。

就我而言,我使用一些可验证的方法来跟踪单元格的高度。

下面是我的代码:-

import UIKit

class WaitingListClass: UIViewController,UITableViewDataSource, UITableViewDelegate {

var selectedCellIndexPath: NSIndexPath!
let SelectedCellHeight: CGFloat = 88.0
let UnselectedCellHeight: CGFloat = 44.0

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
let appdelegateObjForThisClass = UIApplication.sharedApplication().delegate as! AppDelegate
tableView.delegate = self
tableView.dataSource = self
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.backgroundColor = UIColor.clearColor()
self.tableView.separatorColor = tableViewSeperatorColor
self.tableView.tableFooterView = UIView(frame: CGRectZero)
self.tableView.contentInset = UIEdgeInsetsZero
}


internal func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}



internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if noDataForTable == true{
return 1

}
return appdelegateObjForThisClass.nameDataForTable.count
}

internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cellIdentifier = "WaitingCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CellClassWaitingList
cell.nameDisplayLbl.text = appdelegateObjForThisClass.nameDataForTable[indexPath.row]["name"]!
cell.backgroundColor = tableViewCellColor
cell.nameDisplayLbl.textColor = UIColor(red: 191/255, green: 193/255, blue: 192/255, alpha: 1)
cell.idDisplayLbl.textColor = UIColor(red: 48/255, green: 143/255, blue: 94/255, alpha: 1)
cell.idDisplayLbl.text = appdelegateObjForThisClass.indexForTable[indexPath.row]
cell.userInteractionEnabled = true
cell.clipsToBounds = true
cell.selectionStyle = UITableViewCellSelectionStyle.None
let heightOfCell : CGFloat = self.tableView.delegate!.tableView!(self.tableView, heightForRowAtIndexPath: indexPath)
if heightOfCell == self.UnselectedCellHeight{
cell.stackOfBtnInCell.hidden = true
}else if heightOfCell == self.SelectedCellHeight{
cell.stackOfBtnInCell.hidden = false
}
return cell
}

internal func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CellClassWaitingList
if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath == indexPath {
self.selectedCellIndexPath = nil
UIView.animateWithDuration(0.5, animations: {
cell.stackOfBtnInCell.hidden = true
self.view.layoutIfNeeded()
})
} else {
UIView.animateWithDuration(0.5, animations: {
cell.stackOfBtnInCell.hidden = false
self.view.layoutIfNeeded()
})
self.selectedCellIndexPath = indexPath
}
} else {
UIView.animateWithDuration(0.5, animations: {
cell.stackOfBtnInCell.hidden = false
self.view.layoutIfNeeded()
})
selectedCellIndexPath = indexPath
}
tableView.beginUpdates()
tableView.endUpdates()
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CellClassWaitingList
cell.stackOfBtnInCell.hidden = true
}
// MARK: - Cell Separator Setting to Full Size

internal func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if(self.tableView.respondsToSelector(Selector("setSeparatorInset:"))){
self.tableView.separatorInset = UIEdgeInsetsZero
}

if(self.tableView.respondsToSelector(Selector("setLayoutMargins:"))){
self.tableView.layoutMargins = UIEdgeInsetsZero
}

if(cell.respondsToSelector(Selector("setLayoutMargins:"))){
cell.layoutMargins = UIEdgeInsetsZero
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath == indexPath {
return SelectedCellHeight
}
}
return UnselectedCellHeight
}
}

现在我在这里做的是获取一个名为 selectedCellIndexPath 的变量,它将首先获取所选单元格的索引路径。接下来我将使用另外两个名为 SelectedCellHeight 和 UnselectedCellHeight 的变量,它们通常存储一些高度值以便在某些情况下为单元格相互交换 -

  1. 何时滚动表格 View 。
  2. 何时选择和取消选择单元格。

对于其余的实现,请尝试查看 tableView 数据源和委托(delegate)方法。

谢谢,

希望这有帮助。

关于ios - 运行时的 Swift UITableViewCell 动态单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34714338/

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