2016-08-12 12 views
5

Mam indeks sekcji, który zawiera pełny alfabet - jednak sekcje w mojej tabeli zawierają tylko kilka liter (np. C, F, N, U).Swift - jak utworzyć prawidłową sekcjęForSectionIndexTitle?

Chciałbym zmodyfikować sectionForSectionIndexTitle, aby prawidłowo wyświetlać prawidłowe sekcje podczas przesuwania indeksu przekrojów palców. Nadal nie wiem, jak to zrobić za pomocą sekcji.append ...

Dziękuję za pomoc.

ten sposób mój kod wygląda następująco:

Section.swift

struct Section 
{ 
    var heading : String 
    var items : [String] 

    init(title: String, objects : [String]) { 

     heading = title 
     items = objects 
    } 
} 

ViewController.swift

func updateSections(index : Int) { 

    switch index { 

    case 0: 
     sections.append(Section(title: "C", objects: ["Czech Republic"])) 
     sections.append(Section(title: "F", objects: ["France"])) 
     sections.append(Section(title: "N", objects: ["Norway"])) 
     sections.append(Section(title: "U", objects: ["USA"])) 

    default: break 
    } 

    tableViewOutlet.reloadData() 
} 

let arrIndexSection = ["A","B","C","D", "E", "F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"] 

func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { 
    if segmentedControlOutlet.selectedSegmentIndex == 0 { 
     return arrIndexSection 
    } else { 
     return [""] 
    } 
} 

func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { 
    let temp = arrIndexSection as NSArray 
    return temp.indexOfObject(title) 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return sections.count 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let section = sections[section] 
    return section.items.count 
} 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return sections[section].heading 
} 
+0

gdzie wywołujesz funkcję updateSections()? – DeyaEldeen

+0

W wielu miejscach (na przykład podzielona na segmenty lub UISwipeGestureRecognizerDirection). Powyższy kod jest tylko najbardziej odpowiednią częścią związaną z pytaniem. – Heky

+0

jakie jest twoje pytanie? Jak mogę pomóc? –

Odpowiedz

1

try następujący kod w swoim projekcie:

func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { 
    var tempIndex:Int = 0 
    for str in arrIndexSection { 
     if str == title { 
      return tempIndex 
     } 
     tempIndex += 1 
    } 
    return 0 
} 
Powiązane problemy