我在午餐時間裏快速瀏覽了一遍。我原本雖然這可以通過重寫ScrollViewDidScroll來完成,但無法以這種方式工作。我想出的答案是動態決定表是否應該顯示頁腳視圖。
在下面的代碼中(這很粗糙,但應該給出你的想法),我們測試表的行數是否大於我們想要在屏幕上顯示的數量(在這種情況下,我已經硬編碼了這個到10)。如果是,則我們從源數據數組的最後一個元素創建一個頁腳,並將頁腳高度設置爲與其他單元格高度相同的高度,並使用不同的背景顏色。如果沒有,那麼我們用cellForItemAtIndexPath來測試我們是否在數據源的最後一排,在這種情況下,我們相應格式的單元格:
import UIKit
class ViewController: UIViewController {
var myTrackingTableViewController : TrackingTableViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let sourceData : [String] = {
var sD : [String] = [String]()
for count in 0..<50 { // Use the top end of this loop to configure the number of rows in the tableview
sD.append("Number \(count)")
}
return sD
}()
myTrackingTableViewController = TrackingTableViewController(sourceData: sourceData, numberOfRowsOnScreen: 10, style: UITableViewStyle.plain)
self.view.addSubview(myTrackingTableViewController.tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class TrackingTableViewController : UITableViewController {
let sourceData : [String]
var footerText : String!
var footerInline : Bool = false
init(sourceData: [String], numberOfRowsOnScreen: Int, style: UITableViewStyle) {
self.sourceData = sourceData
super.init(style: style)
if self.sourceData.count > numberOfRowsOnScreen {
self.footerText = sourceData.last
}
else
{
footerInline = true
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if footerInline {
return sourceData.count
}
return sourceData.count - 1
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if footerText != nil {
return self.tableView.frame.height/10
}
return 0
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if footerText != nil {
let cell = UITableViewCell()
cell.frame = CGRect(origin: CGPoint(x: 0, y: self.tableView.frame.height - self.tableView.rowHeight), size: CGSize(width: self.tableView.frame.width, height: self.tableView.frame.height/CGFloat(10)))
cell.backgroundColor = UIColor.red
let textLabel = UILabel(frame: CGRect(origin: CGPoint.zero, size: cell.frame.size))
textLabel.text = footerText
cell.addSubview(textLabel)
return cell
}
return nil
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.tableView.frame.height/10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell()
// Clean
for subview in cell.subviews {
subview.removeFromSuperview()
}
// Configure
if indexPath.row == sourceData.count - 1 && footerInline {
cell.backgroundColor = UIColor.red
}
else
{
cell.backgroundColor = UIColor.blue
}
let textLabel = UILabel(frame: CGRect(origin: CGPoint.zero, size: cell.frame.size))
textLabel.text = sourceData[indexPath.row]
cell.addSubview(textLabel)
return cell
}
}
要進行測試,改變爲範圍...循環in viewDidLoad from 0 .. < 5 to 0 .. < 50.你會看到行爲符合你想達到的目的。
希望有幫助!
您是否想過節頁腳視圖?不是表格頁腳視圖,它很好地包裹了你的部分 – Tj3n
你想讓底部單元格看起來像什麼?它是標準單元格上的變體,還是完全不同的東西? – Sparky
@Sparky它可以是兩個。我不介意不使用單元格,也許只是在桌子外面使用uiview或其他東西。但我需要它的行爲像我需要它 – WKL