簡體   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