Swift 3 - 具有第一个单元格的可扩展表格视图已经扩展

时间:2017-03-21 15:04:30

标签: ios swift swift3

我正在使用Swift 3。

我已经按照本教程来获取它,以便我可以点击一个表格视图单元格,这将扩展显示更多信息。

https://www.youtube.com/watch?v=VWgr_wNtGPM&t=294s

我的问题是:我该怎么做才能在视图加载时扩展第一个单元格(即用户不必单击以查看该单元格扩展)但所有其他行为保持不变(例如,如果它再次点击,它会解除崩溃)?

UITableViewCell

import UIKit

class ResultsCell: UITableViewCell {

    @IBOutlet weak var introPara : UITextView!
    @IBOutlet weak var section_heading : UILabel!

    class var expandedHeight : CGFloat { get { return 200.0 } }
    class var defaultHeight : CGFloat { get { return 44.0 } }
    var frameAdded = false


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        section_heading.translatesAutoresizingMaskIntoConstraints = false
    }

    func checkHeight() {
        introPara.isHidden = (frame.size.height < ResultsCell.expandedHeight)
    }

    func watchFrameChanges() {
        if(!frameAdded) {
            addObserver(self, forKeyPath: "frame", options: .new, context: nil)
            checkHeight()
        }
    }

    func ignoreFrameChanges() {
        if(frameAdded){
            removeObserver(self, forKeyPath: "frame")
        }
    }

    deinit {
        print("deinit called");
        ignoreFrameChanges()
    }

    // when our frame changes, check if the frame height is appropriate and make it smaller or bigger depending
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "frame" {
            checkHeight()
        }
    }

}

UITableViewController

// class declaration and other methods above here...

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

// number of rows in the table view
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return section_heading.count
}

// return the actual view for the cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let resultcell = tableView.dequeueReusableCell(withIdentifier: "resultCellTemplate", for: indexPath) as! ResultsCell
    resultcell.section_heading.text = section_heading[indexPath.row]
    resultcell.introPara.attributedText = contentParagraphs[indexPath.row]

    return resultcell
}

// when a cell is clicked
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let previousIndexPath = selectedIndexPath

    // the row is already selected, then we want to collapse the cell
    if indexPath == selectedIndexPath {
        selectedIndexPath = nil
    } else { // otherwise, we expand that cell
        selectedIndexPath = indexPath
    }

    var indexPaths : Array<IndexPath> = []

    // only add a previous one if it exists
    if let previous = previousIndexPath {
        indexPaths.append(previous)
    }
    if let current = selectedIndexPath {
        indexPaths.append(current)
    }

    // reload the specific rows
    if indexPaths.count > 0 {
        tableView.reloadRows(at: indexPaths, with: .automatic)
    }
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    (cell as! ResultsCell).watchFrameChanges()
}

override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    (cell as! ResultsCell).ignoreFrameChanges()

}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath == selectedIndexPath {
        return ResultsCell.expandedHeight
    } else {
        return ResultsCell.defaultHeight
    }
}

所以这可以按预期工作。

enter image description here

但是我如何才能使第一个细胞已经扩展?

感谢您的帮助。

4 个答案:

答案 0 :(得分:3)

我觉得你并不完全理解你自己的代码,但是因为你在你的问题上花了很多精力,我会给你一个提示。

在顶部某处的UITableViewController中,初始化selectedIndexPath,它应该看起来像

var selectedIndexPath: Int?

您可以将其设置为默认值,如此

var selectedIndexPath: Int? = 0

因此,单元格0将在默认情况下展开。

答案 1 :(得分:2)

昨天我已完成类似功能,参考此示例:https://github.com/justinmfischer/SwiftyAccordionCells

根据您的实现,您使用“selectedIndexPath”跟踪当前展开的单元格。因此,当您加载视图时,您必须将“selectedIndexPath”行和section值设置为0,因为您只使用了一个部分。

希望这有用!

答案 2 :(得分:1)

viewDidLoad设置selectedIndexPath = IndexPath(row: 0, section: 0)

应该“自动扩展”第一行。

答案 3 :(得分:0)

看看This,我很久以前就跟着这个了。所以基本上你要设置一个标志System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); System.setProperty("java.naming.provider.url", "localhost:<port>"); ,这样你就可以设置每个单元格是否用完。

通过快速谷歌搜索,here是另一个教程。

相关问题