我有一个加载自定义CollectionViewCells的CollectionViewController。 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,而取决于CollectionlistItem。除了硬编码如果选择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中添加另一个键来指定将在TableViewController的plist2中使用的列表来完成

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

08-05 22:39