Ios 从嵌套数据数组向UITableViewCell内的UITableView填充数据
我是swift和iOS编程新手。我正在尝试制作一个两层的桌面视图。Tableview单元格中的Tableview基本上就是我想说的。主tableview工作正常,但tableview中的tableview根本不工作。。我什么都试过了 #型号Ios 从嵌套数据数组向UITableViewCell内的UITableView填充数据,ios,swift,uitableview,Ios,Swift,Uitableview,我是swift和iOS编程新手。我正在尝试制作一个两层的桌面视图。Tableview单元格中的Tableview基本上就是我想说的。主tableview工作正常,但tableview中的tableview根本不工作。。我什么都试过了 #型号 // Main Model Struct struct MainModel { private(set) public var title: String private(set) public var document: [Documents] =
// 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 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()
}
}
}
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
}
}
#数据服务类
// 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 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()
}
}
}
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
}
}
#主视图控制器
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()
}
}
}
#主表视图单元格类
// 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 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()
}
}
}
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
}
}
#文档TableViewCell类
// 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 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()
}
}
}
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()
}
}
请帮帮我 在
MainTableViewCell
do的updateCell
末尾
self.documentTable.reloadData()
不,先生,那不行。该代码没有对以前的状态进行任何更改..您确定如果调用了let inside cellForRowAt吗?调用了返回Main TableViewCell的cellForRowAt,并且工作正常,但是应该返回documentCell的cellForRowAt,inside Main TableViewCell没有响应。是否使用xib或prototype cell?不仅仅是如果让,但是返回documentCell的整个方法不起作用。。。