我使用斯威夫特3.斯威夫特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
}
}
所以此工程按預期。
但我怎麼讓這個第一個單元格已經展開?
感謝您的幫助。
在發佈的代碼中,'selectedIndexPath'顯然是一個'IndexPath'對象,而不是'Int'。 – DonMag
哦,我明白了,在寫我的答案時,我沒有看到你的帖子。你是對的,它是IndexPath,但我不想在盤子上給出答案,因爲OP似乎不完全理解他自己的代碼。 @DonMag –
謝謝你!這非常有幫助。 – noblerare