Заполните данные в UITableView внутри UITableViewCell из вложенного массива данных - PullRequest
0 голосов
/ 21 сентября 2018

Я новичок в программировании на swift и iOS.Я пытаюсь сделать два слоя таблицы.Tableview внутри ячейки tableview - это то, что я пытаюсь сказатьГлавное представление таблицы работает отлично, но представление таблицы внутри представления таблицы вообще не работает .. Я попробовал все ...

# Модели

// Main Model Struct
struct MainModel {
  private(set) public var title: String
  private(set) public var document: [Documents] = [Documents]()

  init(title: String, document: [Documents]) {
     self.title = title
     self.document = document
  }
}

// Document model struct
struct Documents {
  private(set) public var name: String
  private(set) public var link: String

  init(name: String, link: String){
     self.name = name
     self.link = link
  }
}

# Класс обслуживания данных

class DataService{
   static let instance = DataService()
   private let dataArray = [
       MainModel(title: "Demo 1", document: [Documents(name: "DOC 1", link: "http://www.google.com"), Documents(name: "DOC 2", link: "http://www.facebook.com")])
   ]

   func getArrays() -> [MainModel] {
      return dataArray
   }
}

# Контроллер основного вида

import UIKit

class ResourceDataVC: UIViewController, UITableViewDelegate, UITableViewDataSource {


   @IBOutlet weak var dataTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataTable.delegate = self
        self.dataTable.dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return DataService.instance.getArrays().count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: “mainCell”) as? MainTableViewCell {
            let _item = DataService.instance.getArrays()[indexPath.row]
            cell.updateCell(data: _item)
            return cell
        } else {
            return ResourceCell()
        }
    }
}

# Главный класс TableViewCell

import UIKit

class MainTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var documentTable: UITableView!

    var documentRes: [Documents] = [Documents]()

    override func awakeFromNib() {
        super.awakeFromNib()

        self.documentTable.delegate = self
        self.documentTable.dataSource = self
    }

    func updateCell(data: MainModel) {
        self.title.text = data.title
    self.documentRes = data.document
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.documentRes.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: “documentTableCell”) as? DocumentCell {
            let _doc = documentRes[indexPath.row]
            cell.updateDocument(document: _doc)
            return cell
        } else {
          return DocumentCell()
        }
    }
}

# Document TableViewCell Class

import UIKit

class DocumentCell: UITableViewCell {

    @IBOutlet weak var docName: UILabel!
    @IBOutlet weak var docLink: UILabel!

    func updateDocument(document: Documents) {
        self.docName.text = document.name
    self.docLink.text = document.link
    }

}

И этот метод никогда не вызывается ... почему ??Я думаю, что это может быть проблемой ...

// from: MainTableViewCell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                if let cell = tableView.dequeueReusableCell(withIdentifier: “documentTableCell”) as? DocumentCell {
                    let _doc = documentRes[indexPath.row]
                    cell.updateDocument(document: _doc)
                    return cell
                } else {
                  return DocumentCell()
                }
            }

Пожалуйста, помогите мне!

1 Ответ

0 голосов
/ 21 сентября 2018

В конце updateCell из MainTableViewCell до

self.documentTable.reloadData()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...