2016-07-10 16 views
2

我建立我的第一個iOS應用程序,我想創建這樣的:Swift 2.2 - 如何在(UISegmentedControl的)特定段下創建兩個或多個部分?

  • UISegmentedControl有大約4段(例如所有,電影,電視,音樂)
  • 時如電影被選中,UITableView應該顯示預定義的部分(例如低俗小說,12個憤怒的男人,搏擊俱樂部,黑暗騎士)
  • 每個部分將包含(也是預定義的)細胞(例如在紙漿小說下將存在細胞:John Travolta,昆汀·塔倫蒂諾,烏瑪·瑟曼,塞繆爾·傑克遜)
  • 還,我想有一個段的所有與所有部分和細胞

我能創造的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 
    } 
} 

視圖控制器。迅速

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

實際上你需要一個模型'Movie'(一個自定義的結構或類),它至少包含一個屬性'title'和一個數組''actors'。您的數據源數組包含電影作爲部分,該部分的標題是電影的「標題」,每部分中的行分別是「演員」陣列。 – vadian

+0

我剛剛更新了我的代碼片斷。請你可以檢查一下,讓我知道如何實現變化,以實現我在第一篇文章中描述的內容?謝謝。 – Heky

回答

1

很簡單的例子,根據您的Section結構:

的例子隻影響ViewController類和UISegmentedControl實例應該有兩個部分:Movies(索引0)和Music(指數1 )

  • 爲了方便起見聲明枚舉爲區段索引

  • enum SectionIndex : Int { 
        case Movies, Music 
    } 
    
  • 聲明

    數據源陣列

    var sections = [Section]() 
    
  • 創建方法取決於索引

    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() 
        } 
    
  • sections陣列在viewDidLoad呼叫updateSections與默認索引來填充。

  • 的分段控制的動作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仍然更有效。您只需更改提取請求並重新獲取數據。

+0

它似乎工作正常,非常感謝你!還有一個問題 - 填充「所有」細分的最佳方式是什麼(除了複製粘貼所有部分.append ...)?再次感謝。 – Heky

+0

如果它是一種不可變的數據庫,可能是一個屬性列表文件,但我更喜歡核心數據。 – vadian

相關問題