我正在尝试创建主细节 View 应用程序,但有点卡住了。
我有一个包含 100 个 DynamicPrototype
Cells
的 tableView 显示 Bars,当我点击一个 cell
时,我想显示有关该 Bar 的更多信息.
我的问题是如何将不同的data
(如labels
和images
)填充到DetailView
中并将它们更改为我点击的每个 cell
代表正确的 data
我的 Detailview
是一个带有 StaticCells
的 TableViewController
,如下所示:
我正在使用 Swift 2.3 和 Xcode 8.1
我创建了一个类来表示我的 DetailView 数据:
BarProfile.Swift:
import Foundation
class BarProfile {
var HeaderImage = ""
var HeaderTitle = ""
var Age = ""
var Adress = ""
var Number = ""
var Time = ""
var Music = ""
var Info = ""
var Menu = ""
var More = ""
init(HeaderImage: String, HeaderTitle: String, Age: String, Adress: String, Number: String, Time: String, Music: String, Info: String, Menu: String, More: String) {
self.HeaderImage = HeaderImage
self.HeaderTitle = HeaderTitle
self.Age = Age
self.Adress = Adress
self.Number = Number
self.Time = Time
self.Music = Music
self.Info = Info
self.Menu = Menu
self.More = More
}
}
我的 BarsTableView
看起来像这样:
BarsTableViewController.Swift:
import UIKit
class BarsTableViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate{
@IBOutlet var tableViewController: UITableView!
// MARK : Data
var names = ["Shalvata",
"Markid",
"Litzman Bar",
"The Cat & The Dog",
"Light house",
var streets =
["האנגר 28,נמל תל אביב",
"אבן גבירול 30,תל אביב",
"רחוב נמל תל אביב",
"קרליבך 28,תל אביב",
"האנגר 23,נמל תל אביב"]
var images = [UIImage(named: "Shalvata"),
UIImage(named: "Markid"),
UIImage(named: "Litzman Bar"),
UIImage(named: "CatNDog"),
UIImage(named: "LightHouse")]
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 100.5;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
////////
////////
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let user = filteredUsers[indexPath.row]
cell.photo.image = user.image
cell.name.text = user.name
cell.streetName.text = user.streetName
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("BarsProfile", sender: nil)
}
}
我没有找到显示我真正想要的东西的教程,我希望在这里找到答案,因为 detailView 是每个项目或应用程序的基础。
我不知道如何继续,我对此感到非常沮丧。
感谢您的帮助:)
最佳答案
您可以通过prepareForSegue
函数将数据传递到详细 View ,并在目标 View 中设置变量或调用函数。
这是一个如何完成的示例。这假设您用来呈现“细节”的 View 属于 DetailView
类型,并且来自您的 master -> Detail 的 segue 称为“BarsProfile”:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "BarsProfile") {
if(segue.destination is DetailView) {
let destVC = segue.destination as! DetailView
// Set variables in Detail View here
// e.g. :
destVC.detailData = selectedData
}
}
}
关于ios - Master-DetailView 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711251/