【发布时间】:2017-08-08 00:54:29
【问题描述】:
好的,我正在尝试使我的 CollectionView 的每个部分都有一个标题,并且每个部分中有 3 个带有相应标签的图像视图。我有与标签匹配的图像视图,但我无法返回正确的 numberOfItemsInSection。 我已经多次更改我的代码以试图解决这个问题,我什至不确定我的应用程序的其余部分是正确的,但本质上这是重要的部分:
var sectionsArrayDictionary : [String:Array<String>] = ["level 1" : ["item1", "item2", "item3"], "level 2" : ["item4","item5", "item6"], "level 3" : ["item7", "item8", "item9"]]
我想不出一种方法来访问这些值,以便我可以返回一个计数。 (如果有人知道如何修复,作为额外的帮助:我的图片在每个部分中显示相同的 3 张图片,而不是每个部分都显示自己的图片。我希望在上述部分得到修复后这将解决。)
更新: 我的代码一团糟,但既然你想要更多的代码在这里。目前有两个不同的教程组合在一起,所以可能会让您更加困惑。
import UIKit
let reuseIdentifier = "collCell"
let headerViewIdentifier = "headerView"
var sections: [Section] = SectionsData().getSectionsFromData()
class LayoutController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let sectionInsets = UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0)
var animalLabels = ["max", "roxy", "buster", "daisy", "charlie", "maggie", "buddy", "ruby", "lucky", "bella"]
var animalImages = ["max", "roxy", "buster", "daisy", "charlie", "maggie", "buddy", "ruby", "lucky", "bella"]
var section1 = ["max", "roxy", "buster"]
var section2 = ["daisy","charlie", "maggie"]
var section3 = ["buddy", "ruby", "lucky"]
let sections: [String] = ["Section 1", "Section 2", "Section 3"]
let s1Data: [String] = [item1, item2, item3]
let s2Data: [String] = [item4, item5, item6]
let s3Data: [String] = [item7, item8, item9]
var sectionData: [Int: [String]] = [:]
override func viewDidLoad() {
super.viewDidLoad()
sectionData = [0:s1Data, 1:s2Data, 2:s3Data]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return sections.count
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
let imgName = indexPath.row
cell.animalImage.image = UIImage(named: self.animalImages[imgName])
cell.animalLabel.text = self.animalLabels[imgName]
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return sectionInsets
}
//I don't think this is doing anything...tried to convert it from tableView code
func collectionView(collectionView: UICollectionView, titleForHeaderInSection section:Int) -> String? {
return sections[section]
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView: Header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerViewIdentifier, forIndexPath: indexPath) as! Header
headerView.headerLabel.text = "Level"
return headerView
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print(segue.identifier)
print(sender)
if(segue.identifier == "detail"){
let cell = sender as! CollectionViewCell
let indexPath = collectionView!.indexPathForCell(cell)
let vc = segue.destinationViewController as! DetailViewController
let imgName = animalImages[indexPath!.row]
print(vc)
vc.currImage = UIImage(named: imgName)
vc.textHeading = animalLabels[indexPath!.row]
//
// vc.heading.text = self.titles[0]
// vc.imageView.image = UIImage(named: imgName)
}
}
}
我正在尝试删除 sectionData(其上方有一段代码,其中包含 let 部分、let s1data 和 var 部分)。这根本没有帮助。 我正在尝试用
替换它class SectionsData {
func getSectionsFromData() -> [Section] {
// you could replace the contents of this function with an HTTP GET, a database fetch request,
// or anything you like, as long as you return an array of Sections this program will
// function the same way.
var sectionsArray = [Section]()
let level1 = Section(title: "Level 1", objects: ["max", "roxy", "buster"])
let level2 = Section(title: "Level 2", objects: ["daisy","charlie", "maggie"])
let level3 = Section(title: "Level 3", objects: ["buddy", "ruby", "lucky"])
sectionsArray.append(level1)
sectionsArray.append(level2)
sectionsArray.append(level3)
return sectionsArray
}
}
和 结构部分 {
var heading : String
var items : [String]
init(title: String, objects : [String]) {
heading = title
items = objects
}
}
不幸的是,我不能在主视图页面上将标题、项目、标题或对象称为成员......所以我现在卡住了
【问题讨论】:
-
请出示您的代码。我们无法在没有看到它的情况下修复任何代码。
-
您使用的是 Xcode 7/Swift 2 吗?
-
xcode7 是的,老实说我不知道什么版本的 swift
-
Xcode 7 附带 Swift 2.x,Swift 从 2 到 3 发生了许多变化。我建议您将 Xcode 更新到最新并找到适合更新后的 Xcode 的教程或示例代码.您可能需要升级 macOS,但您可以免费获得 Xcode 和 macOS 升级。 (你只需要足够的磁盘空间来升级。)虽然可能需要一些时间,所以我会尝试在 Swift 2 中写一个答案。
标签: arrays swift dictionary count uicollectionview