详细使用UITableView,如何在TextView中显示字符串(Master-Detail项目,Swift,iOS)

时间:2016-07-18 04:57:45

标签: ios swift uitableview uitextview master-detail

我想使用TableView作为Master Detail应用程序的细节方面。我已经开始使用Xcode中的标准Master Detail项目,删除了它附带的标准应用程序,删除了标准的UIView细节控制器,添加了一个TableView控制器,将TextView添加到原型单元进行测试,并创建了一个新的segue到新的TableView。我将UITableViewCell子类化,并从TextView创建了一个插座(detailTextView)到子类(TableViewCell)。将DetailViewController.swift中的类从UIViewController更改为UITableViewController。我成功地将字符串stringForTextView = "String for TextView"从master传递给详细信息。但我无法弄清楚如何在TextView中显示该字符串。我试图通过插座(detailTextView.text)在详细视图中引用TextView文本,但得到了"Use of unresolved identifier detailTextView"

非常感谢任何帮助。

相关代码如下所示。

如果有帮助,你也可以在这里下载整个项目: http://greendept.com/MasterDetailTwoTableViews/

TableViewCell.swift(详细原型单元的子类)

import UIKit

class TableViewCell: UITableViewCell {

    @IBOutlet weak var detailTextView: UITextView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

DetailViewController.swift

import UIKit

class DetailViewController: UITableViewController {

    var stringForTextView : String?

    var detailItem: AnyObject? {
        didSet {
            // Update the view.
            self.configureView()
        }
    }

    func configureView() {
        // THE NEXT TWO LINES WORK: PASSED IN STRING PRINTS TO CONSOLE
        let printThis = stringForTextView! as String
        print("\(printThis)")
        // BUT THE REFERENCE TO THE OUTLET BELOW DOES NOT WORK, GIVES
        // "Use of unresolved identifier detailTextView"
        detailTextView.text = printThis
        }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.configureView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

MasterViewController.swift

import UIKit

class MasterViewController: UITableViewController {

    var detailViewController: DetailViewController? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if let split = self.splitViewController {
            let controllers = split.viewControllers
            self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
        }
    }

    override func viewWillAppear(animated: Bool) {
        self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: - Segues

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.stringForTextView = "String for TextView"
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    }

    // MARK: - Table View

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        return cell
    }

    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
}

2 个答案:

答案 0 :(得分:0)

DetailViewControllerUITableViewController,您无法访问tableView控制器中的detailTextView。您在单元格中定义了插座,您可以在此处访问和配置detailTextView

DetailViewController作为UITableViewController是没有任何意义的,如果您真正想要的是在那里配置文本视图。然后,您应该将其设置回UIViewController,并将文本视图作为单个UITextView添加到视图控制器视图。

答案 1 :(得分:0)

下面的链接显示了如何更改单元格标签中的文本,即使textview的出口位于单元子类中。它用一个TableView显示了这个。

creating custom tableview cells in swift

在我的测试项目中采用上述方法时,我根本不需要更改Master。在详细信息视图中,configureView()不执行更新TextView的主要工作。这发生在cellForRowAtIndexPath - 第二个到详细视图中的最后一个函数。另一个区别是我不能,也不需要实现@IBOutlet var tableView: UITableView! - 因为tableView已经可以作为存储属性使用。我还必须在几个地方添加overrride。最后,在TableViewCell类中,我添加了一个链接到TextView内容视图的插座。结果是TextView文本正在更新。

TableViewCell.swift:

import UIKit   
class TableViewCell: UITableViewCell {

    @IBOutlet weak var detailTextView: UITextView!
    @IBOutlet weak var detailContentView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        print ("awakeFromNib")
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state

        print("test")
    }
}

DetailViewController.swift:

import UIKit

class DetailViewController: UITableViewController {

    // @IBOutlet var tableView: UITableView! -- cannot override a stored property

    var stringForTextView : String?

    // Don't forget to enter this in IB also
    let cellReuseIdentifier = "reuseIdentifier"

    var detailItem: AnyObject? {
        didSet {
            // Update the view.
            self.configureView()
        }
    }

    func configureView() {
        // Update the user interface for the detail item.
            // stringForTextView
        let printThis = stringForTextView! as String
        print("\(printThis)")
 //       detailTextView.text = printThis
        }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        self.configureView()
    }

    // needed "override" here
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return 1
    }

    // create a cell for each table view row
    // needed "override" here
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell:TableViewCell = self.tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as! TableViewCell

        cell.detailTextView.text = stringForTextView

        print("cell.detailTextView.text: \(cell.detailTextView.text)")

        print("row : \(indexPath.row)")

        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}