[英]Dynamic UITableView Datasource inside of Static UITableView Cell
可能有更好的方法来执行此操作,但是我整天都在搜寻Google,并且无法找到针对我的特定问题的解决方案:
我有一个静态TableView,它显示大量信息,并包括一个文本输入部分,用于向嵌套在父静态TableView的一个单元格中的动态TableView添加值。
为此,我在UITableViewController中有一个UITableViewDataSource和UITableViewDelegate的子类,它们将控制嵌套TableView中显示的内容。 我有一个到嵌套表的出口:
@IBOutlet weak var validatedCardsTable: UITableView!
在我的viewDidLoad
有:
let dataSource = ValidatedCardsDataSource(tableView: self.validatedCardsTable)
self.validatedCardsTable.dataSource = dataSource
self.validatedCardsTable.delegate = dataSource
我的子类如下所示:
class ValidatedCardsDataSource :NSObject, UITableViewDataSource, UITableViewDelegate {
var tableView:UITableView
init(tableView : UITableView) {
self.tableView = tableView
super.init()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 1
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = "Title of Row: #\(indexPath.row)"
return cell
}
}
当我运行此代码时,嵌套的TableView会加载我在cellForRowAtIndexPath
设置的适当的“行标题”文本(尽管tableview的大小没有调整,但我确定是另一个问题)。 但是,当我在任何地方按下TableView时,我在情节提要中的单元格原型中也指定了一个“ A Label”标签,该单元格上也会显示该标签,然后当我释放该按钮时,所有格式都会消失,并且单元格会还原到默认状态。 我假设我没有完全将某些东西挂在某个地方,但是您可以提供任何帮助来跟踪我在做什么的错误,或者提出一种更好的方法来解决此问题,将不胜感激! 屏幕截图如下:
原型单元:
负载显示什么:
着陆时显示的内容:
修饰后显示的内容:
覆盖单元格方法“ touchesBegan:withEvent:/ touchesEnded:withEvent:/ touchesCancelled:withEvent:”当单元格被上下/上下/上下触摸时,请更改UI样式。
经过更多的实验,我能够弄清楚! 回顾起来似乎很明显,但是当我实例化数据源时,我做了一些奇怪的循环引用可笑性。 我删除了init
函数,并删除了我在dataSource子类中使用的tableView参数的引用。 这是我的dataSource类现在的样子:
let dataSource = ValidatedCardsDataSource()
self.validatedCardsTable.dataSource = dataSource
self.validatedCardsTable.delegate = dataSource
class ValidatedCardsDataSource :NSObject, UITableViewDataSource, UITableViewDelegate {
var selectedRow = 0
let rowCount = 10
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 1
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = "Title of Row: #\(indexPath.row)"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(indexPath.row)
self.selectedRow = indexPath.row
tableView.reloadData()
}
}
现在,我只需要弄清楚我的限制...感谢您的答复!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.