Swift 2.2 - 如何在特定段(UISegmentedControl)下创建两个或更多部分?

时间:2016-07-10 13:41:56

标签: ios swift uitableview uisegmentedcontrol

我正在构建我的第一个iOS应用,我想创建以下内容:

  • UISegmentedControl,包含大约4个片段(例如全部,电影,电视,音乐)
  • 例如电影被选中,UITableView应该显示预定义的部分(例如低俗小说,12个愤怒的男人,搏击俱乐部,黑暗骑士)
  • 每个部分都将包含(也是预定义的)细胞(例如在Pulp Fiction下会有细胞:John Travolta,Quentin Tarantino,Uma Thurman,Samuel L. Jackson)
  • 另外,我想要一个包含所有部分和单元格的分段

我能够创建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
}

1 个答案:

答案 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 DataNSFetchedResultController会更有效率。您只需更改获取请求并重新获取数据。