我想创建一个动态表,该动态表在被选择时会扩展另一个数据。
我已经按照本教程 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)!)
}
}
}
}
}
如何解决此重复数据? 谢谢。