swift - 如何根据特定的第一个 Collection View 选择转到第二个 TableView Controller (从 plist)?

标签 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 中的项目时,我需要 View 转到由单独的 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)
}

关于swift - 如何根据特定的第一个 Collection View 选择转到第二个 TableView Controller (从 plist)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38300400/

相关文章:

ios - 如果无法打开 URL,则显示警报

ios - 如何在 iOS Swift 中进行多线程、并发或并行?

ios - 我在 Tableview 单元格中有 collectionView。它总是返回零。我该如何解决?

ios - UICollectionView - 何时/如何使用自动布局获取单元格内 View 的大小?

swift - 为什么 swift 不允许在三元组中进行基于 let 的决策?

ios - Swift 3 - inout 字符串不能转换为字符串

iphone - NSFetchedResultsChangeDelete 未被触发

ios - 来自不同部分的 TableView 单元格混合在一起

objective-c - IOS滑动删除和多行编辑导致按钮名称设置为默认

ios - Swift:按屏幕分页不适用于两列布局 Collection View