好的,我正在尝试制作我的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部分,让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
}
}
和 struct Section {
var heading : String
var items : [String]
init(title: String, objects : [String]) {
heading = title
items = objects
}
}
不幸的是,我无法在主视图页面上将标题,项目,标题或对象称为成员...所以我现在卡住了
答案 0 :(得分:0)
给出一个部分字符串。
let count = sectionsArrayDictionary["level 1"].count
我知道必须有更多内容,因此您需要发布更多代码,以便我们更好地了解问题。
<强>更新强>
所以我们仍然缺少Section
的关键声明,但假设您可以直接访问其objects
属性;这就是你所需要的。
let count = sections[1].objects.count
对于二级对象daisy,charlie和maggie,count变量现在应为3。展望未来,你应该只为Swift 3开发,很快就会开发4.不要在这个项目的中途切换,但 nobody 正在使用Swift 2进行开发。任何在线Swift 2教程都已过时。祝你好运。
答案 1 :(得分:0)
首先,如何从字典中获取计数:
class DummyPage extends Component {
constructor(props) {
super(props)
this.realmUpdated = this.realmUpdated.bind(this);
realm.objects('todoItem').addListener('change', this.realmUpdated);
}
componentWillUnmount() {
realm.objects('todoItem').removeListener('change', this.realmUpdated);
}
realmUpdated() {
this.forceUpdate();
}
render() {
//build your data source in here and render the sectionList
}
}
有些观点:
var sectionsArrayDictionary : [String:Array<String>] = ["level 1" : ["item1", "item2", "item3"], "level 2" : ["item4","item5", "item6"], "level 3" : ["item7", "item8", "item9"]]
let sectionIndex = 0
let sectionName = "level \(sectionIndex+1)" //->level 1
if let sectionItems = sectionsArrayDictionary[sectionName] {
let count = sectionItems.count
print(count)
} else {
print("No entry for '\(sectionName)'")
}
)的返回类型是可选的,上面的代码使用Optional Binding来处理它[]
代码块中,true
只是sectionItems
,因此您只需使用Array<String>
属性来获取计数但是,当您采用新的count
时,可能不需要以上所有内容。
在将代码调整为新的SectionsData
之前,稍加修改一下:
SectionsData
在您的代码中,您将单独处理每个项目的标签文本和图像名称,因此引入了struct Item {
var imageName: String
var labelText: String
}
struct Section {
var heading : String
var items : [Item]
init(title: String, objects : [String]) {
heading = title
items = objects.map {Item(imageName: $0, labelText: $0)}
}
}
类型以单独保留它们。
Item
在应用中经常使用,因此我将其重命名为Data
。同时也不建议在每次使用时创建实例, Singleton模式是目前的首选方式。
SectionsManager
我还添加了一种方法,可以从class SectionsManager {
//### Singleton
static let shared = SectionsManager()
private init() {}
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
}
func getSectionsFromDictionary(dictionary: [String: [String]]) -> [Section] {
var sectionsArray = [Section]()
//### `.sort({$0.0 < $1.0})` sorts the dictionary by its key.
for (title, objects) in dictionary.sort({$0.0 < $1.0}) {
//print(title, objects) //See what is happening while debugging.
let level = Section(title: title, objects: objects)
sectionsArray.append(level)
}
return sectionsArray
}
}
创建Array<Section>
。
通过以上所有准备工作,您的sectionsArrayDictionary
会变成这样:
LayoutController
您可能需要一些修复才能使此代码在您的项目中运行,但我希望它不会太多,您可以自己完成。
答案 2 :(得分:0)
这里使用this获取sectionArrayDictionary中的数组总数
var sectionsArrayDictionary : [String:Array<String>] = ["level 1" : ["item1", "item2", "item3"], "level 2" : ["item4","item5", "item6"], "level 3" : ["item7", "item8", "item9"]]
var keys = Array(sectionsArrayDictionary.keys)
print(keys)
print(keys.count) //Output - 3