我有一个 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/