I have UITableViewController
with 2 dynamic cells(one for a cell with detail and one for UIPickerView). Programatically I create 2 sections with 1 row(1 section) and 2 row(2 section). Rows open up UIPickerView
when you tap on them. But I have a little problem. I don't know how to force UIPickerView
reload/update and show needed data. UIPickerView
load an only the array of cells that I open up first. If I open up another cell(with another array) UIPickerView
doesn't reload data. I tried to put reloadAllComponents()
anywhere, but nothing happens. Do you have any idea how I can fix this? Thanks
import Foundation
import UIKit
class VC: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var units = ["A","B","C","D","E","F","G"]
var units2 = ["1","2","3","4","5","6","7"]
var units3 = ["I","II","III","IV","V","VI","VII"]
// Show or hide picker
var showUnitsPickerView = false
var showUnits2PickerView = false
var showUnits3PickerView = false
var pickerView = UIPickerView()
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var count = 0
if showUnitsPickerView {
count = units.count
} else if showUnits2PickerView {
count = units2.count
} else {
count = units3.count
}
return count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var value = ""
if showUnitsPickerView {
value = String(units[row])
} else if showUnits2PickerView {
value = String(units2[row])
} else if showUnits3PickerView {
value = String(units3[row])
}
return value
}
// MARK: - View Did Load
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count = 1
switch section {
case 0:
if showUnitsPickerView {
count = 2
}
case 1:
if showUnits3PickerView
|| showUnits2PickerView {
count = 3
} else {
count = 2
}
default:
count = 1
}
return count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
var universallCell = UITableViewCell()
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TitleAndDetailCellT3.self), for: indexPath) as! TitleAndDetailCellT3
cell.title.text = "Picker 1"
cell.detail.text = "A"
universallCell = cell
} else if showUnitsPickerView {
let cellWithPickerView = tableView.dequeueReusableCell(withIdentifier: String(describing: PickerViewCellT3.self), for: indexPath) as! PickerViewCellT3
cellWithPickerView.pickerView.delegate = self
cellWithPickerView.pickerView.dataSource = self
cellWithPickerView.pickerView = pickerView
universallCell = cellWithPickerView
}
return universallCell
case 1:
var universallCell = UITableViewCell()
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TitleAndDetailCellT3.self), for: indexPath) as! TitleAndDetailCellT3
let cellWithPickerView = tableView.dequeueReusableCell(withIdentifier: String(describing: PickerViewCellT3.self), for: indexPath) as! PickerViewCellT3
switch indexPath.row {
case 0:
cell.title.text = "Picker 2"
cell.detail.text = "1"
universallCell = cell
case 1:
if showUnits2PickerView {
cellWithPickerView.pickerView.delegate = self
cellWithPickerView.pickerView.dataSource = self
cellWithPickerView.pickerView = pickerView
universallCell = cellWithPickerView
} else {
cell.title.text = "Picker 3"
cell.detail.text = "I"
universallCell = cell
}
case 2:
if showUnits2PickerView {
cell.title.text = "Picker 3"
cell.detail.text = "I"
universallCell = cell
} else if showUnits3PickerView {
cellWithPickerView.pickerView.delegate = self
cellWithPickerView.pickerView.dataSource = self
cellWithPickerView.pickerView.reloadAllComponents()
cellWithPickerView.pickerView = pickerView
universallCell = cellWithPickerView
}
default:
break
}
return universallCell
default:
let universallCell = UITableViewCell()
return universallCell
}
// Configure the cell...
//return universallCell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
struct RowName {
var units = 0
var units2 = 1
var units3 = 2
}
// Hide row
func hideRow(rowName: Int) {
switch rowName {
case RowName().units:
if showUnitsPickerView {
showUnitsPickerView = false
tableView.beginUpdates()
tableView.deleteRows(at: [
(NSIndexPath(row: 1, section: 0) as IndexPath)], with: .automatic)
tableView.endUpdates()
}
case RowName().units2:
if showUnits2PickerView {
showUnits2PickerView = false
tableView.beginUpdates()
tableView.deleteRows(at: [
(NSIndexPath(row: 1, section: 1) as IndexPath)], with: .automatic)
tableView.endUpdates()
}
case RowName().units3:
if showUnits3PickerView {
showUnits3PickerView = false
tableView.beginUpdates()
tableView.deleteRows(at: [
(NSIndexPath(row: 2, section: 1) as IndexPath)], with: .automatic)
tableView.endUpdates()
}
default:
break
}
}
switch indexPath.section {
case 0:
hideRow(rowName: RowName().units2)
hideRow(rowName: RowName().units3)
showUnitsPickerView = showUnitsPickerView ? false : true
if showUnitsPickerView {
tableView.deselectRow(at: indexPath, animated: true)
tableView.beginUpdates()
tableView.insertRows(at: [
(NSIndexPath(row: 1, section: 0) as IndexPath)], with: .automatic)
self.pickerView.reloadAllComponents()
tableView.endUpdates()
} else {
tableView.deselectRow(at: indexPath, animated: true)
tableView.beginUpdates()
tableView.deleteRows(at: [
(NSIndexPath(row: 1, section: 0) as IndexPath)], with: .automatic)
tableView.endUpdates()
}
case 1:
if indexPath.row == 0 {
tableView.deselectRow(at: indexPath, animated: true)
hideRow(rowName: RowName().units)
hideRow(rowName: RowName().units3)
showUnits2PickerView = showUnits2PickerView ? false : true
if showUnits2PickerView {
tableView.beginUpdates()
tableView.insertRows(at: [
(NSIndexPath(row: 1, section: 1) as IndexPath)], with: .automatic)
tableView.endUpdates()
} else {
tableView.beginUpdates()
tableView.deleteRows(at: [
(NSIndexPath(row: 1, section: 1) as IndexPath)], with: .automatic)
tableView.endUpdates()
}
} else {
tableView.deselectRow(at: indexPath, animated: true)
hideRow(rowName: RowName().units)
hideRow(rowName: RowName().units2)
showUnits3PickerView = showUnits3PickerView ? false : true
if showUnits3PickerView {
tableView.beginUpdates()
tableView.insertRows(at: [
(NSIndexPath(row: 2, section: 1) as IndexPath)], with: .automatic)
tableView.endUpdates()
} else {
tableView.beginUpdates()
tableView.deleteRows(at: [
(NSIndexPath(row: 2, section: 1) as IndexPath)], with: .automatic)
tableView.endUpdates()
}
}
default:
break
}
}
}