gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:13:58 24 4
gpt4 key购买 nike

我想将 TableView 用于主详细信息应用程序的详细信息部分。我从 Xcode 中的标准 Master Detail 项目开始,删除了它附带的标准应用程序,删除了标准 UIView 细节 Controller ,添加了 TableView Controller ,向原型(prototype)单元添加了一个 TextView 进行测试,并创建了一个新的 segue新的 TableView 。我对 UITableViewCell 进行了子类化,并创建了一个从 TextView 到子类 (TableViewCell) 的导出 (detailTextView)。将 DetailViewController.swift 中的类从 UIViewController 更改为 UITableViewController。我成功地将字符串 stringForTextView = "String for TextView" 从 master 传递到细节。但我不知道如何在 TextView 中显示该字符串。我尝试通过 socket (detailTextView.text)引用详细 View 中的 TextView 文本,但得到“使用未解析的标识符detailTextView”

任何帮助将不胜感激。

相关代码如下所示。

如果有帮助,您还可以在此处下载整个项目: http://greendept.com/MasterDetailTwoTableViews/

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

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
}
}

最佳答案

DetailViewController是一个UITableViewController,你无法访问tableView Controller 中的detailTextView。您在单元格中定义了导出,您可以在其中访问和配置 detailTextView

如果您真正想要的是在那里配置 TextView ,那么将 DetailViewController 作为 UITableViewController 没有任何意义。然后,您应该将其设置回 UIViewController,并将 TextView 作为单个 UITextView 添加到 View Controller View 。

关于ios - 详细使用UITableView,如何在TextView中显示字符串(Master-Detail项目,Swift,iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38428841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com