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

时间:2016-07-11 06:26:05

标签: swift uitableview uicollectionview plist

我有一个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 - &gt; CollectionViewCell1(来自plist1) - &gt; TableView1(来自plist2)

CollectionView - &gt; CollectionViewCell2(来自plist1) - &gt; TableView2(来自plist2)

评论是否需要更多澄清或细节,因为我发现这很难以完全清晰的方式描述。

1 个答案:

答案 0 :(得分:1)

我认为这可以通过在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)
}