简体   繁体   English

静态tableview单元内的动态Tableview

[英]Dynamic Tableview inside a Static tableview Cell

I wanna populate a dynamic tableview inside a static tableview cell, by the same class for both of these. 我想通过相同的类在静态表格视图单元格中填充动态表格视图。 As you can see in the picture under the cell 'GRE Test Information'. 如您在图片中看到的,“ GRE测试信息”单元格下方。

帮助图片

I'm using the code inside the the class named as MenuController, which is a tableview controller. 我正在使用名为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.
}

But it's not populating that at runtime, and says 但这不是在运行时填充的,并且说

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cellNew - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法使标识符为cellNew的单元出队-必须为该标识符注册一个笔尖或一个类或在情节提要中连接原型单元”

However I'm using the same identifier named as cellNew both in the code and storyboard. 但是,我在代码和情节提要中都使用了名为cellNew的相同标识符。

Well after astonishing efforts regarding this one, I've found the solution. 在为此做出惊人的努力之后,我找到了解决方案。 Concerning the following: Swift: TableView within Static UITableViewCell 关于以下内容: Swift:静态UITableViewCell中的TableView

Where the problem solver says : As far as I can determine by experimenting with this, you can't use the same UITableViewController as the data source and delegate of both table views. 问题解决者说的是: 根据我的经验,您不能将相同的UITableViewController用作数据源和两个表视图的委托。 With a static table view, you're not supposed to implement the data source methods at all. 在静态表视图中,您根本不需要实现数据源方法。 The strange thing is, even if I disconnect the data source and delegate connections between my static table view and the table view controller, that table view still calls numberOfRowsInSection in my table view controller class. 奇怪的是,即使我断开了数据源并委托了静态表视图和表视图控制器之间的连接,该表视图仍会在表视图控制器类中调用numberOfRowsInSection。 If I explicitly set the data source to nil in code, that stops it from calling the data source methods, but the embedded dynamic table view also fails to call them, so this structure doesn't work. 如果我在代码中将数据源显式设置为nil,这将阻止它调用数据源方法,但是嵌入式动态表视图也无法调用它们,因此此结构不起作用。

However, you can get around this by using a different object to be the data source and delegate of your embedded dynamic table view. 但是,您可以通过使用其他对象作为嵌入式动态表视图的数据源和委托来解决此问题。 Make an IBOutlet for your embedded table view, and set its data source and delegate to this new object (The class is DataSource in this example, and it's a subclass of NSObject). 为您的嵌入式表视图创建IBOutlet,并将其数据源设置为该新对象(此示例中的类为DataSource,并且是NSObject的子类)。

I've modified my code in this way now : 我现在以这种方式修改了代码:

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
  }

}

Now it works exactly fine. 现在,它可以正常工作。

这是截图显示的结果

in viewDidLoad 在viewDidLoad中

// First Register the UITableViewcell class from nib 
let cellNib = UINib(nibName: "MainTableViewCell", bundle: bundle)
self.tableView.registerNib(cellNib, forCellReuseIdentifier:"cellNew")

Then Check with below screeshots 然后用下面的截图检查

STEP 1 : Select MainTableViewCell from Identity Inspector-Custom Class-Click Class Drop Down arrow.It shows you list.From that you can click the MainTableViewCell 步骤1 :从Identity Inspector-Custom Class-Click Class下拉箭头中选择MainTableViewCell,它会显示您的列表,然后您可以单击MainTableViewCell

在此处输入图片说明

STEP 2 :Once you click that it shows the name with selected table view cell. 步骤2 :单击后,它将显示带有选定表视图单元格的名称。

在此处输入图片说明

While the existing answers explain how you can do this, they don't address whether you should do this. 现有答案说明了如何执行此操作,但并未解决您是否应该执行此操作。 From the example you provided, it seems that all you need is a single UITableView with multiple dynamic cell types. 从你提供的例子,似乎所有你需要的是一个单一 UITableView与多个动态的细胞类型。 Each cell type can specify its contentInsets to indent the content as needed. 每种单元格类型都可以指定其contentInsets以根据需要缩进内容。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cellNew - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法使标识符为cellNew的单元出队-必须为该标识符注册一个笔尖或一个类或在情节提要中连接原型单元”

However I'm using the same identifier named as cellNew both in the code and storyboard. 但是,我在代码和情节提要中都使用了名为cellNew的相同标识符。

You're getting this error because you are dequeing/retrieving the prototype cell from the wrong table! 您收到此错误是因为您从错误的表中取出/取出了原型单元!

The line in your cellForRowAtIndexPath should be: 您的cellForRowAtIndexPath中的行应为:

let cell = tablle.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! MainTableViewCell

Having said that, even once that is working, asking a tableViewController to act as data source and delegate for both a static and a dynamic table causes problems later. 话虽如此,即使这样行​​之有效,要求tableViewController充当数据源并为静态和动态表委派也将在以后引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM