gpt4 book ai didi

ios - 带有静态单元格的 DetailView

转载 作者:行者123 更新时间:2023-11-28 16:03:23 26 4
gpt4 key购买 nike

我正在尝试创建主细节 View 应用程序,但有点卡住了。

我有一个包含 100 个 DynamicPrototype Cells 的 tableView 显示 Bars,当我点击一个 cell 时,我想显示有关该 Bar 的更多信息.

我的问题是如何将不同的data(如labelsimages)填充到DetailView 中并将它们更改为我点击的每个 cell 代表正确的 data

我的 Detailview 是一个带有 StaticCellsTableViewController,如下所示:

Bars My Friends

我正在使用 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

}
}

感谢您的帮助:)

最佳答案

这很简单,你只是认为你在 viewController1 中显示 headerTitle,如果你在 detailTableViewController 中选择任何行,你将要显示ageaddressnumber(您可以通过创建额外的静态单元格并在其中放置标签来显示您想要的任何东西。)

您修改的类:

首先在init()方法中也声明所有属性为可选和可选

import Foundation

class BarProfile {
var HeaderImage: String?
var HeaderTitle: String?
var Age: String?
var Adress: String?
var Number: String?
var Time: String?
var Music: String?
var Info: String?
var Menu: String?
var More: String?

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

}
}

像往常一样填充 ViewController1 TableView ,当用户点击任何单元格时,将该选定对象传递给 detailTableViewController。从 ViewController1 中的 TableView 单元格创建 segue 到 detailTableViewController

ViewController1 中的代码:

class viewController1: UIViewController, UITableViewDataSource, UITableViewDelegate {

var barArray:[BarProfile] = []
var selctedIndexPath: NSIndexPath?

override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...5 { // Creating dummy object for clear understanding.
let barObject = BarProfile(HeaderImage: "werw", HeaderTitle: "erqwrw", Age: "34", Adress: "qwerwer", Number: "32", Time: "time", Music: "qerqw", Info: nil, Menu: nil, More: nil)
barArray.append(barObject)
}
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return barArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell Identifier")! as UITableViewCell
cell.textLabel?.text = barArray[indexPath.row].HeaderTitle
return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selctedIndexPath = indexPath
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: Navigation Methods

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueIdentifier" {
let detailViewController = segue.destinationViewController as? detailTableViewController
detailViewController?.selectedBarObject = barArray[(selctedIndexPath?.row)!]
}
}

}

在您的 detailTableViewController 中编写代码:

class detailTableViewController: UITableViewController {

var selectedBarObject: BarProfile?

@IBOutlet var ageLabel: UILabel! // connect these outlets to your label in static cells.
@IBOutlet var addressLabel: UILabel!
@IBOutlet var numberLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
if selectedBarObject != nil {
showBarDetails()
} else {
// Do nothing
}
}

// You are going to use static cells so no need to implement table view data source methods

func showBarDetails() {
ageLabel.text = selectedBarObject?.Age
addressLabel.text = selectedBarObject?.Adress
numberLabel.text = selectedBarObject?.Number
}

}

谢谢:)

关于ios - 带有静态单元格的 DetailView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40667054/

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