繁体   English   中英

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

[英]Swift UITableViewCell Dynamic Cell Height on Runtime

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

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

解决方案:我将UITableViewUITableViewCell子类化。 每次selected/tapped 选项”时,我都会调用beginUpdateendUpdate ,它们运行良好。

问题:每次用户向下滚动时,选项均无法正确显示。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

每当您向上或向下滚动时,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的变量,该变量将首先使用所选单元格的indexpath。 接下来,我还要使用另外两个变量,称为SelectedCellHeight和UnselectedCellHeight,它们通常存储一些高度值以在某些情况下为单元格相互交换,例如-

  1. 当tableview将被滚动。
  2. 何时选择和取消选择单元格。

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

谢谢,

希望这会有所帮助。

您需要保留单元格状态的列表,并在表视图数据源cellAtIndexPath:heightForCellAtIndexPath:分别设置单元格的格式并指定所需的单元格高度。

您可以创建一个NSMutableSet并向其中添加/删除NSIndexPath对象,或者使用NSMutableDictionary并检查与单元格索引相对应的某个键是否具有值。

您是什么意思,“选项显示不正确”?

另外,我该怎么做:

  1. 通过情节提要在UITableView中创建另一个UITableViewCell。 该TableView单元具有您需要的自定义高度。
  2. 创建一个新的UITableViewCell类文件。
  3. 将新类分配到情节提要中的单元格。
  4. 按下选项时,调用TableView.reloadData()。 然后将所选单元格的布尔值设为true。
  5. 当Bool为true时,在cellForRowAtIndexPath()中使用自定义UITableViewCell类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM