【问题标题】:Master-DetailView ApplicationMaster-DetailView 应用程序
【发布时间】:2023-04-08 13:57:01
【问题描述】:

我正在尝试创建一个主详细信息视图应用程序,但我有点卡住了。

我有一个 tableView,其中有 100 个 DynamicPrototype Cells 显示 Bars,当我点击 cell 时,我想提供有关该 Bar 的更多信息。

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

我的DetailviewTableViewControllerStaticCells,如下所示:

我正在使用 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方法。
  • 我还没有
  • 那你需要做更多的教程,然后问具体问题。试试看:techotopia.com/index.php/…
  • 你写得很好,适合新手。不用担心。请参阅this moment,尽管我建议您观看整个视频。太棒了

标签: ios iphone swift uitableview master-detail


【解决方案1】:

您可以通过prepareForSegue 函数将数据传递到详细视图,并在目标视图中设置变量或调用函数。

这是一个如何完成的示例。这假设您用于呈现“详细信息”的视图是 DetailView 类型,并且来自您的主人的 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
            }
        }
    }

【讨论】:

  • 如果我这样做,我的 didselectrow 保持不变?
  • 我不明白这个问题。我的理解是您想在“主”视图的表中选择一行,这会打开相应的“详细”视图并用所选项目的信息填充它。这是正确的吗?
  • 是的。 @RicketyRick
猜你喜欢
  • 2012-03-24
  • 1970-01-01
  • 2013-01-07
  • 2018-07-31
  • 2012-03-31
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
相关资源
最近更新 更多