gpt4 book ai didi

arrays - 快速传递具有不同内容的数组

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

我很苦恼,希望有人能帮助我。我有一个包含六个按钮的 View Controller 。根据用户单击的按钮, TableView 中应显示不同的内容。我创建了数组,结构始终相同。我的第一个连接到 Storyboard 中的 View Controller 的Viecontroller被命名为:IntroViewController。第二个名为“Cat1TableViewcontroller”。

我认为我已经完成了所有必要的步骤。程序运行,但是当我单击其中一个按钮时,表是空的。

这是我为“第一个”ViewControllerFile 所做的事情:

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

}

我的“第二个” View Controller 将数组(在操作函数中)复制到一个空数组并进一步使用它(以便将数据传递到下一个 View Controller )

这是该类的一部分,应仅显示用户通过单击按钮选择的数组内容。

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
}

我很高兴获得任何帮助。

谢谢。

最佳答案

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

class DataFactory {

var contentMode = 0

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

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

您可以像这样在 ViewController 中访问此服务:

 //Setting the content mode
DataFactory.sharedInstance.contentMode = 1

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

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

示例:

//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];

关于arrays - 快速传递具有不同内容的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48567875/

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