我正在构建我的第一个iOS应用,我想创建以下内容:
我能够创建UITableView,例如段电影显示带有单元格的一个部分(电影)。但是,我不知道如何在细分受众群下创建多个部分。
仅供参考,用户不会修改或删除部分和单元格。
我试图在网上找到答案,但并没有真正成功。
谢谢你的帮助,伙计们!
修改 我现在在项目中有一些代码片段(部分和对象的名称不同 - 这只是一个例子)。有两个主要问题: 1.每个部分只显示一个部分(同名)。我计划稍后添加更多的部分/对象。我应该在ViewController.swift中做出哪些更改来显示更多部分? 2.我不得不重复ViewController.swift中的部分(例如,让pulpFiction:[String] = ...)来填充单元格,因为我不知道如何在SectionsData.swift中访问它们。我知道必须有更好的方法来做到这一点 - 拜托,你能告诉我吗?
Section.swift
struct Section {
var heading : String
var items : [String]
init(title: String, objects : [String]) {
heading = title
items = objects
}
}
SectionsData.swift
var segment1Count: Int!
var segment2Count: Int!
var segment3Count: Int!
let section1 = Section(title: "Pulp Fiction", objects: ["John Travolta", "Quentin Tarantino", "Uma Thurman", "Samuel L. Jackson"])
let section2 = Section(title: "12 Angry Men", objects: ["Henry Fonda", "Lee J. Cobb", "Martin Balsam", "John Fiedler"])
let section3 = Section(title: "Fight Club", objects: ["Edward Norton", "Brad Pitt"])
class SectionsData {
func getSectionsFromData() -> [Section] {
var sectionsArray = [Section]()
sectionsArray.append(section1)
sectionsArray.append(section2)
sectionsArray.append(section3)
segment1Count = section1.items.count
segment2Count = section2.items.count
segment3Count = section3.items.count
return sectionsArray
}
}
ViewController.swift
var sections: [Section] = SectionsData().getSectionsFromData()
let pulpFiction: [String] = ["John Travolta", "Quentin Tarantino", "Uma Thurman", "Samuel L. Jackson"]
let angryMen: [String] = ["Henry Fonda", "Lee J. Cobb", "Martin Balsam", "John Fiedler"]
let fightClub: [String] = ["Edward Norton", "Brad Pitt"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
var sectionsNumber = 0
switch(segmentedControlOutlet.selectedSegmentIndex) {
case 0:
sectionsNumber = sections.count
break
case 1:
sectionsNumber = 1
break
case 2:
sectionsNumber = 1
break
case 3:
sectionsNumber = 1
break
default:
break
}
return sectionsNumber
}
internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var numberOfRows: Int!
switch(segmentedControlOutlet.selectedSegmentIndex) {
case 0:
numberOfRows = sections[section].items.count
break
case 1:
numberOfRows = segment1Count
break
case 2:
numberOfRows = segment2Count
break
case 3:
numberOfRows = segment3Count
break
default:
break
}
return numberOfRows
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
var sectionHeader: String!
switch(segmentedControlOutlet.selectedSegmentIndex) {
case 0:
sectionHeader = sections[section].heading
break
case 1:
sectionHeader = segmentedControlOutlet.titleForSegmentAtIndex(1)
break
case 2:
sectionHeader = segmentedControlOutlet.titleForSegmentAtIndex(2)
break
case 3:
sectionHeader = segmentedControlOutlet.titleForSegmentAtIndex(3)
break
default:
break
}
return sectionHeader
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("prototypeCell", forIndexPath: indexPath)
switch(segmentedControlOutlet.selectedSegmentIndex) {
case 0:
cell.textLabel!.text = sections[indexPath.section].items[indexPath.row]
break
case 1:
cell.textLabel!.text = pulpFiction[indexPath.row]
break
case 2:
cell.textLabel!.text = angryMen[indexPath.row]
break
case 3:
cell.textLabel!.text = fightClub[indexPath.row]
break
default:
break
}
return cell
}
答案 0 :(得分:1)
基于Section
结构的非常简单的示例:
该示例仅影响ViewController
类,UISegmentedControl
实例应该包含两个部分:Movies
(索引0)和Music
(索引1)
为方便起见,声明了段索引的枚举
enum SectionIndex : Int {
case Movies, Music
}
声明数据源数组
var sections = [Section]()
根据索引
创建一个填充sections
数组的方法
func updateSections(index : SectionIndex)
{
sections.removeAll()
switch index {
case .Movies:
sections.append(Section(title: "Pulp Fiction", objects: ["John Travolta", "Quentin Tarantino", "Uma Thurman", "Samuel L. Jackson"]))
sections.append(Section(title: "12 Angry Men", objects: ["Henry Fonda", "Lee J. Cobb", "Martin Balsam", "John Fiedler"]))
sections.append(Section(title: "Fight Club", objects: ["Edward Norton", "Brad Pitt"]))
case .Music:
sections.append(Section(title: "The Beatles", objects: ["John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr"]))
sections.append(Section(title: "Genesis", objects: ["Phil Collins", "Mike Rutherford", "Tony Banks"]))
sections.append(Section(title: "Queen", objects: ["Freddie Mercury", "Brian May", "Roger Taylor", "John Deacon"]))
}
tableView.reloadData()
}
在viewDidLoad
调用updateSections
并使用默认索引。
override func viewDidLoad() {
super.viewDidLoad()
updateSections(.Movies)
}
将分段控件的操作valueChanged
连接到此IBAction
@IBAction func didChangeValue(control : UISegmentedControl)
{
updateSections(SectionIndex(rawValue:control.selectedSegmentIndex)!)
}
这些是表视图数据源和委托方法
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let section = sections[section]
return section.items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("prototypeCell", forIndexPath: indexPath)
let section = sections[indexPath.section]
cell.textLabel!.text = section.items[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].heading
}
现在,当您点击分段控件时,sections
数组会相应更新,并重新加载表视图。但是,使用Core Data
和NSFetchedResultController
会更有效率。您只需更改获取请求并重新获取数据。