[英]Tableview Controller with a dynamic tableview in its static cell
[英]Dynamic Tableview inside a Static tableview Cell
我想通过相同的类在静态表格视图单元格中填充动态表格视图。 如您在图片中看到的,“ GRE测试信息”单元格下方。
我正在使用名为MenuController的类中的代码,MenuController是一个表视图控制器。
class MenuController: UITableViewController,MFMailComposeViewControllerDelegate {
@IBOutlet weak var tablle: UITableView!
var items = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
items = ["A "," BB "]
tablle.delegate = self
tablle.dataSource = self
self.tablle.registerClass(MainTableViewCell.self, forCellReuseIdentifier: "cellNew")
}
// Table Data Source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! MainTableViewCell
print("Aasim Khaan")
cell.customCell01.text = items[indexPath.row]
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
但这不是在运行时填充的,并且说
由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法使标识符为cellNew的单元出队-必须为该标识符注册一个笔尖或一个类或在情节提要中连接原型单元”
但是,我在代码和情节提要中都使用了名为cellNew的相同标识符。
在为此做出惊人的努力之后,我找到了解决方案。 关于以下内容: Swift:静态UITableViewCell中的TableView
问题解决者说的是: 根据我的经验,您不能将相同的UITableViewController用作数据源和两个表视图的委托。 在静态表视图中,您根本不需要实现数据源方法。 奇怪的是,即使我断开了数据源并委托了静态表视图和表视图控制器之间的连接,该表视图仍会在表视图控制器类中调用numberOfRowsInSection。 如果我在代码中将数据源显式设置为nil,这将阻止它调用数据源方法,但是嵌入式动态表视图也无法调用它们,因此此结构不起作用。
但是,您可以通过使用其他对象作为嵌入式动态表视图的数据源和委托来解决此问题。 为您的嵌入式表视图创建IBOutlet,并将其数据源设置为该新对象(此示例中的类为DataSource,并且是NSObject的子类)。
我现在以这种方式修改了代码:
import Foundation
import UIKit
import MessageUI
class DataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
var items : [String] = ["GRE Test Structure ","GRE Score "]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! MainTableViewCell
cell.customCell01.text = items[indexPath.row]
return cell
}
}
class MenuController: UITableViewController,MFMailComposeViewControllerDelegate {
@IBOutlet var tablle0: UITableView!
@IBOutlet weak var tablle: UITableView!
var dataSource = DataSource()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem =
self.editButtonItem()
tablle.delegate = dataSource
tablle.dataSource = dataSource
}
}
现在,它可以正常工作。
在viewDidLoad中
// First Register the UITableViewcell class from nib
let cellNib = UINib(nibName: "MainTableViewCell", bundle: bundle)
self.tableView.registerNib(cellNib, forCellReuseIdentifier:"cellNew")
然后用下面的截图检查
步骤1 :从Identity Inspector-Custom Class-Click Class下拉箭头中选择MainTableViewCell,它会显示您的列表,然后您可以单击MainTableViewCell
步骤2 :单击后,它将显示带有选定表视图单元格的名称。
现有答案说明了如何执行此操作,但并未解决您是否应该执行此操作。 从你提供的例子,似乎所有你需要的是一个单一 UITableView
与多个动态的细胞类型。 每种单元格类型都可以指定其contentInsets
以根据需要缩进内容。
由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法使标识符为cellNew的单元出队-必须为该标识符注册一个笔尖或一个类或在情节提要中连接原型单元”
但是,我在代码和情节提要中都使用了名为cellNew的相同标识符。
您收到此错误是因为您从错误的表中取出/取出了原型单元!
您的cellForRowAtIndexPath中的行应为:
let cell = tablle.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! MainTableViewCell
话虽如此,即使这样行之有效,要求tableViewController充当数据源并为静态和动态表委派也将在以后引起问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.