如何根据特定的第一个集合视图选择转到第二个表视图控制器(来自 plist)?

How to go to a second tableview controller (from plist) based on specific first collectionview selection?

我有一个 CollectionViewController 加载自定义 CollectionViewCells。 CollectionViewCell 中的元素由 plist 文件填充:

plist1:

<array>
    <dict>
        <key>Title</key>
        <string>Example Title</string>
        <key>Description</key>
        <string>Short description...</string>
        <key>Time</key>
        <string>Feb 6, 4:45</string>
        <key>Background</key>
        <string>Default</string>
    </dict>
    <dict>
        <key>Title</key>
        <string>Example Title 2</string>
        <key>Description</key>
        <string>Short description...</string>
        <key>Time</key>
        <string>Feb 6, 4:45</string>
        <key>Background</key>
        <string>Default2</string>
    </dict>
</array>

当 CollectionView 中的项目被选中时,我需要视图转到由单独的 plist 文件填充的新 TableViewController。加载的 TableViewController 应该取决于选择的 CollectionViewItem,这取决于 plist 条目。如果选择了 IndexRow 1/2/etc,除了硬编码之外,我更喜欢一种方法来执行此操作。

plist2:

<dict>
    <key>List1</key>
    <array>
        <string>Item1</string>
        <string>Item2</string>
        <string>Item3</string>
    </array>
    <key>List2</key>
    <array>
        <string>Item1</string>
        <string>Item2</string>
        <string>Item3</string>
    </array>
</dict>

基本上:

CollectionView -> CollectionViewCell1(来自 plist1) -> TableView1(来自 plist2)

CollectionView -> CollectionViewCell2(来自 plist1) -> TableView2(来自 plist2)

如果需要更多说明或细节,请发表评论,因为我发现这很难完全清楚地描述。

我认为这可以通过在 plist1 中添加另一个键来指定将在 plist2 中用于 TableViewController 的列表来完成

<array>
<dict>
    <key>Title</key>
    <string>Example Title</string>
    <key>Description</key>
    <string>Short description...</string>
    <key>Time</key>
    <string>Feb 6, 4:45</string>
    <key>Background</key>
    <string>Default</string>
    <key>ListName</key>
    <string>List1</string>
</dict>
<dict>
    <key>Title</key>
    <string>Example Title 2</string>
    <key>Description</key>
    <string>Short description...</string>
    <key>Time</key>
    <string>Feb 6, 4:45</string>
    <key>Background</key>
    <string>Default2</string>
    <key>ListName</key>
    <string>List2</string>
</dict>

在 TableViewController 中添加一个变量

var listName = ""
override func viewDidLoad() {
    // get list from plist2

    // then reload table
}

和您的 prepareForSegue 函数

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let indexPathRow = sender as! Int
    let destination = segue.destinationViewController as! TableViewController
    destination.listName = // get your list name from plist1 based on index path row
}

最后选中单元格,然后执行segue

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // pass which indexpath row is selected
    performSegueWithIdentifier("showTableView", sender: indexPath.row)
}