首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传递不同内容的数组

传递不同内容的数组
EN

Stack Overflow用户
提问于 2018-02-01 16:51:13
回答 1查看 40关注 0票数 0

我在挣扎,我希望有人能帮助我。我有一个视图控制器,包含六个按钮。根据用户单击的按钮,应该在表视图中显示不同的内容。我已经创建了数组,结构总是相同的。我的firstViecontroller连接到故事板中的视图控制器,名为: IntroViewController。第二个名为“Cat1TableViewcontroller”。

我以为我已经做了许多必要的步骤。程序运行,但是当我单击其中一个按钮时,表是空的。

以下是我为我的“第一个”ViewControllerFile所做的工作:

代码语言:javascript
复制
class IntroViewController: UIViewController
{
 var contentMode = 0

@IBAction func draussen(_ sender: Any)
{
    contentMode = 1
    performSegue(withIdentifier: "showTable", sender: self)
}


@IBAction func kiez(_ sender: Any)
{
    contentMode = 2
    performSegue(withIdentifier: "showTable", sender: self)
}

@IBAction func fahren(_ sender: Any)
{
    contentMode = 3
     performSegue(withIdentifier: "showTable", sender: self)
}


@IBAction func photo(_ sender: Any)
{
    contentMode = 4
     performSegue(withIdentifier: "showTable", sender: self)
}


@IBAction func essen(_ sender: Any)
{
    contentMode = 5
     performSegue(withIdentifier: "showTable", sender: self)

}

@IBAction func schwitzen(_ sender: Any)
{
    contentMode = 6
     performSegue(withIdentifier: "showTable", sender: self)
}

override func viewDidLoad()
{
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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

}

我的“第二个”视图控制器将数组(在操作函数中)复制到一个空数组中,并进一步使用它(以便将数据传递给下一个视图控制器)。

下面是类中应该只显示用户通过单击按钮选择的数组内容的部分。

代码语言:javascript
复制
class Cat1TableViewController: UITableViewController
{
var spots:[Spot] = []
var spot2:[Spot] = [

    Spot(name:"Rye Playland", type:"Vergnügungspark am Strand",description:"hjhjhjhjhjhjhjhjhjhjhhhjh",location:"Rye",phone:"78787878787",image:"skyline", isVisited: false),
    Spot(name:"Splish Splash", type:"Wasserpark",description:"hjhjhjhjhjjhjhjjh",location:"Long Island",phone:"7878787878777878",image:"skyline", isVisited: false),
    Spot(name:"Kotti Berliner Döner", type:"Türkisches Essen",description:"hjhjhjhjhjhjhjhjhjjhjhjhjhjhjhjj",location:"Brooklyn",phone:"78787878787878787878",image:"skyline", isVisited: false),
    Spot(name:"Die größte Pizzascheibe der Welt", type:"Italienisches Essen",description:"jhjhjhjhjhjhjhjhjhjhjhj",location:"Yonkers",phone:"78787878787878",image:"skyline", isVisited: false),
    Spot(name:"Die billigste Pizzascheibe der Welt", type:"Italienisches Essen",description:"hjhjhjhjhjhjhjhjhjhjhjhjhjhjhjjh", location:"Manhattan",phone:"8989898989",image:"skyline", isVisited: false)

    ]

 var spot3:[Spot] = [
    Spot(name:"Sate in Queens", type:"Asiatisches Essen",description:"hjhjhjhjhjhjhjh",location:"Queens",phone:"787878787787", image:"skyline", isVisited: false),
    Spot(name:"Intrepid", type:"Museum",description:"hjhjhjhjhjhjhjhjh",location:"Manhattan",phone:"787878787878787",image:"skyline", isVisited: false),
    Spot(name:"Fire Island", type:"Strand",description:"hjhjhjhjhjhj",location:"Long Island",phone:"78788787878787",image:"skyline", isVisited: false),
    Spot(name:"Jones Beach", type:"Strand",description:"hjhjhjhjjhjh",location:"Long Island",phone:"78787878787878787878", image:"skyline", isVisited: false)

    ]
var spot4:[Spot] = [

Spot(name:"Waterfront in Yonkers", type:"Aussicht",description:"jhjhjhjhjhjhjjh",location:"Yonkers",phone:"8989898989899", image:"skyline", isVisited: false),
Spot(name:"Dim Sum in Queens", type:"Asiatisches Essen",description:"hjhjhjjhjhjhjhjhjhjhjhjh",location:"Queens",phone:"7878787878787", image:"skyline", isVisited: false),
Spot(name:"Koreanisches Spa", type:"Wellness",description:"hjhjhjhjhjhjhjhjhjhjhjhjhjhjhjhj",location:"Queens",phone:"787878787878878",image:"skyline", isVisited: false)
]


 var spot5:[Spot] = [
Spot(name:"Queens Botanischer Garten", type:"Botanischer Garten",description:"hjhjhjhjhjhjhjhjhjhj", location:"Queens",phone:"78787878787878",image:"skyline", isVisited: false),
Spot(name:"Open Air Kino", type:"Kino",description:"hjhjhjhjhjhjhjhjhjhjhjhjhj",location:"Brooklyn",phone:"89989898989898", image:"skyline", isVisited: false)
]


var spot6:[Spot] = [
    Spot(name:"Orchard Beach", type:"Strand",description:"jkjkjkjkjkkjkkjkjkjkjkjkjkjkjkjkjkj",location:"Bronx",phone:"7878787878", image:"skyline", isVisited: false),
    Spot(name:"Paddeln am Hudson River", type:"Natur und Bewegung",description:"hjhjhjhjhjhjhjhjhjhjhjhjhjhjhj",location:"Hudson",phone:"7787878787878787",image:"skyline", isVisited: false),
    Spot(name:"Umsonst und Draussen", type:"Washington Park",description:"hjhjhjhjhjhjhjhjhhhhhhhhghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghgjhjh",location:"Manhattan",phone:"8989898989898", image:"skyline", isVisited: false)

]




override func numberOfSections(in tableView: UITableView) -> Int
{
    // we tell the table view that there is only one section and return the total number of spots as stored in the array

    if IntroViewController().contentMode == 1
    {
        spots = spot1
    }

    if IntroViewController().contentMode == 2
    {
        spots = spot2
    }

    if IntroViewController().contentMode == 3
    {
        spots = spot3
    }

    if IntroViewController().contentMode == 4
    {
        spots = spot4
    }
    if IntroViewController().contentMode == 5
    {
        spots = spot5
    }
    if IntroViewController().contentMode == 6
    {
        spots = spot6
    }

    return spots.count

}

override func tableView(_ tableView: UITableView, cellForRowAt 
indexPath: IndexPath)->UITableViewCell
{
    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: 
cellIdentifier, for: indexPath) as! SpotTableViewCell// because I am 
using a custom cell I am converting to the specific cell type
    // configure the cell

    if IntroViewController().contentMode == 1
    {
        spots = spot1

    }

    if IntroViewController().contentMode == 2
    {
        spots = spot2
    }

    if IntroViewController().contentMode == 3
    {
        spots = spot3
    }

    if IntroViewController().contentMode == 4
    {
        spots = spot4
    }
    if IntroViewController().contentMode == 5
    {
        spots = spot5
    }
    if IntroViewController().contentMode == 6
    {
        spots = spot6

    }

    cell.nameLabel.text = spots[indexPath.row].name
    cell.locationLabel.text = spots[indexPath.row].location
    cell.typeLabel.text = spots[indexPath.row].type
    cell.thumbnailImageView.image = 
  UIImage(named:spots[indexPath.row].image)

    //cell.heartImageView.isHidden = spots[indexPath.row]isVisited ? 
 false : true
    return cell
}

我很高兴能得到任何帮助。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-01 17:08:19

您应该将应该从不同UIViewControllers访问的数据存储在一个单独的服务中,如下所示:

代码语言:javascript
复制
class DataFactory {

    var contentMode = 0

    static let sharedInstance : DataFactory = {
        let instance = DataFactory()
        return instance
    }()

    var spot2:[Spot] = []
    ...

而不是像这样在ViewControllers中访问该服务:

代码语言:javascript
复制
 //Setting the content mode
 DataFactory.sharedInstance.contentMode = 1

 //Getting the result array 
 let yourArray = DataFactory.sharedInstance.spot2

您还可以将数据存储在数组中,并可以使用contentMode指示器作为索引。

示例:

代码语言:javascript
复制
//two dimensional array of spots
var spots = [[Spot]]();

//than you can store your spot arrays inside this two dimensional array
spots.append(spot2);

//accessing the spots via contentMode-Index (I would rename the contentMode to spotsIndex or something similar)
let currentSpots = spots[self.contentMode];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48567875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档