Я хочу создать динамическую таблицу, которая расширяет другие данные, когда она выбрана.
Я следую этому уроку https://www.youtube.com/watch?v=ClrSpJ3txAs , но я не могу сделать динамический (каждый данныедублирует)
Вот что я хочу сделать
А вот что я сделал
Сам данныедублирует, и я не знаю, как это исправить, потому что, когда я изменяю некоторый код внутри tableView (), он падает, когда я выбираю его или когда я перехожу к этому представлению.
Вот мой код:
import UIKit
import PopupDialog
struct cellData {
var isOpened = Bool()
var title = [String]()
var sectionData = [String]()
}
class TutorialBankTableViewController: UITableViewController {
var user = DBManager.instance.getUserProfile()
var tableViewData = [cellData]()
var extendedData = ""
var sendTitleNameForGettingExtendedData = ""
var arrayextendedData: [String] = []
var titleName: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
reloadTableView()
// Do any additional setup after loading the view.
}
func reloadTableView() {
if titleName.isEmpty == true {
requestTitleName()
} else {
tableViewData = [cellData(isOpened: false, title: titleName, sectionData: arrayextendedData),
cellData(isOpened: false, title: titleName, sectionData: arrayextendedData),
cellData(isOpened: false, title: titleName, sectionData: arrayextendedData),
cellData(isOpened: false, title: titleName, sectionData: arrayextendedData),
cellData(isOpened: false, title: titleName, sectionData: arrayextendedData)]
DispatchQueue.main.async { self.tableView.reloadData() }
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return tableViewData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if tableViewData[section].isOpened {
return (tableViewData[section].sectionData.count + 1)
}
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
return UITableViewCell()
}
cell.textLabel?.text = tableViewData[indexPath.section].title[indexPath.row]
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
return UITableViewCell()
}
self.tableView.contentInset = UIEdgeInsetsMake(0, 16, 0, 0);
cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
return cell
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if tableViewData[indexPath.section].isOpened {
let indexPath = tableView.indexPathForSelectedRow //optional, to get from any UIButton for example
let currentCell = tableView.cellForRow(at: indexPath!) as! UITableViewCell
print(currentCell.textLabel!.text)
sendTitleNameForGettingExtendedData = currentCell.textLabel!.text!
requestExtendedData()
} else {
tableViewData[indexPath.section].isOpened = true
}
let sections = IndexSet(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
func requestTitleName(){
NetworkingService.shared.reqTitle(phone_number: user!.phone_number ?? "") { spinnerResponse in
if (spinnerResponse == nil){
self.view.makeToast("Technical problem, please try again...")
} else {
if (spinnerResponse?.success == Const.ResponseKey.Success){
print(spinnerResponse ?? "")
if let apiResponseData = spinnerResponse!.data{
self.titleName.removeAll()
// filtering data what I want to put
_ = apiResponseData.filter { (dic) -> Bool in
if dic.types == “Testing Request“ {
self.titleName.append(dic.listName ?? "")
}
return true
}
}
print(self.titleName)
self.reloadTableView()
} else if (spinnerResponse?.success == Const.ResponseKey.SessionEnd) {
self.sessionEnd()
} else{
let popup = PopupDialog(title: "Gagal Meminta Data ", message: spinnerResponse?.error)
let buttonOK = DefaultButton(title: "OK"){
}
popup.addButton(buttonOK)
self.present(popup,animated: true,completion: nil)
}
}
}
}
func requestExtendedData() {
print(TransactionNumber.instance.genTransNumber(_jenisTransaksi: .BANK, _phoneNumber: (user?.phone_number)!))
NetworkingService.shared.requestExtendedDataCashin(phone_number: user!.phone_number!, jenis_trx: "VI", nama_lk: sendTitleNameForGettingExtendedData) { responseTutorial in
if (responseTutorial == nil) {
self.view.makeToast("Technical problem, please try again...")
} else {
if responseTutorial?.success == Const.ResponseKey.Success {
self.extendedData = (responseTutorial?.token)!
// convert into array for showing all text in responseTutorial.token
var delimiter = "\n"
self.arrayextendedData = (responseTutorial?.token!.components(separatedBy: delimiter))!
self.reloadTableView()
} else if responseTutorial?.success == Const.ResponseKey.SessionEnd {
self.sessionEnd()
} else if responseTutorial?.success == Const.ResponseKey.Error {
self.view.makeToast((responseTutorial?.token)!)
}
}
}
}
}
Как я могу исправить это дублирование данных?Спасибо.