
我有兩個按鈕計劃拍攝和完成拍攝以及它下面的表格視圖。我不能在這里使用分段控制。當我按下第一個按鈕時,表格視圖應該填充資料。當我按下第二個按鈕時,表格視圖應該填充不同的資料。我怎樣才能做到這一點。我是iOS新手如何解決這個問題?
uj5u.com熱心網友回復:
為您的資料創建兩個單獨的陣列,一個用于計劃的拍攝,另一個用于已完成的拍攝 維護一個布爾變數以在它們之間切換
var scheduledShoots: [YourModel] = [...]
var completedShoots: [YourModel] = [...]
var isScheduledShootSelected = true
然后在您的表格視圖方法中執行此操作
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return isScheduledShootSelected ? scheduledShoots.count : completedShoots.count
}
也在你的 cellForRowAt 檢查 isScheduledShootSelected 并填充相應的資料
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if isScheduledShootSelected {
//Populate scheduledShoots array data
} else {
//Populate completedShoots array data
}
return yourCell
}
現在在您的操作方法中
@IBAction func scheduledShootPressed() {
isScheduledShootSelected = true
tableView.reloadData()
}
@IBAction func completedShootPressed() {
isScheduledShootSelected = false
tableView.reloadData()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/480749.html
