- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试拥有 UITableView
(主要)其中包含另一个 UITableView
(次要)在每个单元格中。主要和次要 TableView 都有自己的数据源对象类,MajorDatasource 对象包含 MinorDatasource 对象的数组,如下所示。
我已在 UITableViewDatasource
中放置了断点每个类的函数,看起来像 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
MinorDatasource
的功能从未被调用,即使 labels
数组包含对象。
据我了解,datasource
UITableView
的属性很弱,因此需要在某个地方对其进行强有力的引用,但是,我认为在这种情况下这是正确的,我在这里做错了什么吗?
class MajorDatasource: NSObject, UITableViewDataSource {
var minorDatasources:[MinorDatasource]
init(minorDatasources:[MinorDatasource]) {
self.minorDatasources = minorDatasources
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return minorDatasources.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MajorTableViewCell")! as! MajorTableViewCell
cell.tableView.dataSource = minorDatasources[indexPath.row]
return cell
}
}
class MinorDatasource: NSObject, UITableViewDataSource {
var labels:[String]
init(labels:[String]) {
self. labels = labels
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MinorTableViewCell")!
cell.textLabel?.text = labels[indexPath.row]
return cell
}
}
最佳答案
您还没有显示其余的代码 - 单元类、 Controller 类等 - 因此很难说您可能缺少什么或做错了什么。
因此,根据您包含的代码以及您的描述,这里是一个工作示例,应该非常接近您想要的内容。
结果:
全部通过代码进行,因此没有@IBOutlets
之类的...只需创建一个新的UIViewController
并将其类指定为MajorMinorViewController
:
//
// MajorMinorViewController.swift
//
// Created by Don Mag on 9/13/19.
//
import UIKit
class MajorTableViewCell: UITableViewCell {
var tableView: UITableView = {
let v = UITableView()
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() -> Void {
contentView.backgroundColor = .yellow
contentView.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor, constant: 0.0),
tableView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: 0.0),
tableView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor, constant: 0.0),
tableView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor, constant: 0.0),
tableView.heightAnchor.constraint(equalToConstant: 205.0),
])
tableView.register(MinorTableViewCell.self, forCellReuseIdentifier: "MinorTableViewCell")
}
}
class MinorTableViewCell: UITableViewCell {
// default UITableViewCell
}
class MajorDatasource: NSObject, UITableViewDataSource {
var minorDatasources:[MinorDatasource]
init(minorDatasources:[MinorDatasource]) {
self.minorDatasources = minorDatasources
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return minorDatasources.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MajorTableViewCell")! as! MajorTableViewCell
cell.tableView.dataSource = minorDatasources[indexPath.row]
return cell
}
}
class MinorDatasource: NSObject, UITableViewDataSource {
var labels:[String]
init(labels:[String]) {
self.labels = labels
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MinorTableViewCell") as! MinorTableViewCell
cell.textLabel?.text = labels[indexPath.row]
return cell
}
}
class MajorMinorViewController: UIViewController {
var tableView: UITableView = {
let v = UITableView()
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
var minorLabels: [[String]] = [
["One", "Two", "Three", "Four", "Five", "Six", "Seven"],
["A", "B", "C", "D", "E", "F", "G", "H"],
["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
]
var majorDatasource: MajorDatasource!
var minorDatasources:[MinorDatasource] = [MinorDatasource]()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0),
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0.0),
tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0.0),
tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0.0),
])
for labelArray in minorLabels {
let mds = MinorDatasource(labels: labelArray)
minorDatasources.append(mds)
}
tableView.register(MajorTableViewCell.self, forCellReuseIdentifier: "MajorTableViewCell")
majorDatasource = MajorDatasource(minorDatasources: minorDatasources)
tableView.dataSource = majorDatasource
}
}
关于swift - 嵌套 UITableViewDataSource 不调用 cellForRowAt :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57919270/
我已经检查了其他几个问题,答案似乎在数据源方法的声明中。但是,我找不到我的方法声明有什么问题。 这是我的类(class): import UIKit class GuestControl: UITab
到目前为止,我很喜欢 MonoTouch,但是一旦您通过了几个初学者类(class),就会发现缺少文档和示例。 我正在努力解决在 MonoTouch 中填充简单 UITableView 的简单概念。我
在我们的应用程序中,我们有几个 UITableView,它们具有网络支持的数据源,实际填充这些数据源可能需要一些时间。以下是通常发生的情况: 用户触发 View 。 在 viewDidLoad 中,创
我有一个 UITableView,它应该呈现分成部分 的条目。数据需要一些时间来处理,因此它是这样完成的: @interface AsyncTable() NSMutableArray* _alp
数据源方法没有被触发我的 tableView 仍然是空的。使用断点,我可以看到数据源从未执行过我不明白为什么以及如何 import UIKit class SearchResultsViewContr
class EmployeeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var emp
我有一个 NSArray 从服务器获取的歌曲。那是我的原始数据源。该数组包含以 NSDictionary 和 NSArray 作为后端的自定义对象。 我想知道使用 NSDictionary 实现格式化
尝试将我的表的值分配给 self 时出现以下错误。 Cannot assign a value of type 'ActivityViewController' to a value of type
如何在 Swift 中定义协议(protocol):( @protocol Protocol - (Class)generateObject; @end 编辑:如果我想在 OC 中定义一个函数,如下
我在我的一个 UITableViews 中遇到索引越界异常,我认为可能是一些多线程问题。这是我认为正在发生的事情: 我有一个 UITableView,它的数据源是一个普通的 NSMutableArra
我在连接到 TodayViewController 的 ViewController 中使用 UITableView。我想使用我的 Parse 数据库中的数据加载到 TableView 中。 这是我的
我从 Xcode 模板创建了一个新的主从应用程序 我发现主界面(在本例中为 BirdMasterViewController)继承自 UITableViewController,和 UITableVi
我写代码有困难,协议(protocol)出错,我用的是xcode 7.3.1 //2 Method dari protokol UITableViewDataSource->method 1. fun
目前,我在我的应用程序中设置了以下内容: 例如,我有一个游戏列表,其中的游戏有不同的状态,我希望某个状态位于某个部分等。 因为我有 3 种不同的状态,所以我将使用 NSDictionary,其中 St
由于 UITableView 在 UIViewController 中,我创建了一个符合 UITableViewDataSource 协议(protocol)的类,并将其附加到 UIViewContr
我的项目中有 4 个文件: myTableViewController.swift myTableViewDataSource.swift myCustomCell.swift 我的自定义单元格.xi
我在 Xcode 8 beta 中编写了 tableView 的代码,然后尝试在实际的 Xcode 7 中执行它。除了 UITableViewDataSource 问题外,我下面的代码看起来是正确的。
问题:如何通过扩展编写 UITableViewDataSource 的默认实现? Swift支持协议(protocol)扩展中的默认实现,UITableViewDataSource是一个协议(prot
我设置了 tableview 数据源,但它无法识别他的错误类型“ViewController”不符合协议(protocol)“UITableViewDataSource” import UIKit c
因此,我在 Xcode 中收到“Type ViewController does not conform to protocol UITableViewDataSource”错误。我已经实现了所需的功
我是一名优秀的程序员,十分优秀!