从Swift中的前一个视图控制器捕获操作

时间:2015-11-07 10:54:41

标签: arrays swift controller segue cell

我是编程的初学者,我试图在mu第二视图控制器中填充单元格,具有两个不同的数组取决于在前一个视图控制器中单击了哪个部分。我不知道如何从前一个控制器中捕获动作,以及在不同情况下应该使用哪种方式在第二个viewController中填充Cell。

////这很好..

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("MainCellIdentifier", forIndexPath: indexPath)
    cell.textLabel?.text = self.mainSectionsArray[indexPath.section]

    // Configure the cell...

    return cell
}
//MARK: - UITableViewDelegate

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

         self.performSegueWithIdentifier("popularSpotIdentifier", sender: self) 

///但这里没有..

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.favouriteMuseumArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("CityCellIdentifier", forIndexPath: indexPath)

    let selectedSection = indexPath.section
    if  selectedSection == 0 {
        cell.textLabel?.text = self.favouriteCitySpotsArray[indexPath.row].name

    } else if selectedSection == 1 {
        cell.textLabel?.text = self.favouriteMuseumArray[indexPath.row].name
    }
    return cell

}

我试图将选定的部分设置为indexPath.section但是因为在前一个视图中发生了这种情况,控制器的结果与上面的代码不一样。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

好的,如果有人遇到同样的问题,这里就是答案。

//我最近将表视图更改为集合视图,但它们的方法基本相同

这里我使用instantiateViewControllerWithIdentifier方法初始化下一个视图控制器即时推送。

//主视图控制器

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 2
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MainCellIdentifier", forIndexPath: indexPath) as! MainCellCollectionViewCell
    cell.mainLabel.text = self.mainSectionsArray[indexPath.row]
    cell.mainImageView?.image = self.imageArray[indexPath.row]
    return cell
}

//MARK: - UICollectionViewDelegate

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    if (indexPath.row == 0) {
        let favSpot = self.storyboard?.instantiateViewControllerWithIdentifier("FavouriteControllerID") as! FavouriteCollectionViewController
        favSpot.selectedSpot = 0
        navigationController?.pushViewController(favSpot, animated: true)
    } else {
        let museumSpot = self.storyboard?.instantiateViewControllerWithIdentifier("FavouriteControllerID") as! FavouriteCollectionViewController
        museumSpot.selectedSpot = 1
        navigationController?.pushViewController(museumSpot, animated: true)
    }

我也设置了// selectedSpot的值,所以我可以在下一个视图控制器中使用它

//下一个视图控制器

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    selectedRow = indexPath.row
    if selectedSpot == 0 {
        let mapCitySpot = self.storyboard?.instantiateViewControllerWithIdentifier("MapStoryboardID") as! MapViewController
        let citySpot = popularSpotArray[selectedRow]
        mapCitySpot.favouriteSpot = citySpot
        navigationController?.pushViewController(mapCitySpot, animated: true)

    } else {
        let mapMuseumSpot = self.storyboard?.instantiateViewControllerWithIdentifier("MapStoryboardID") as! MapViewController
        let museumSpot = museumSpotArray[selectedRow]
        mapMuseumSpot.favouriteSpot = museumSpot
        navigationController?.pushViewController(mapMuseumSpot, animated: true)
    }
}