2016-08-12 13 views
5

Tôi có chỉ mục phần có chứa bảng chữ cái đầy đủ - tuy nhiên, các phần trong bảng của tôi chỉ chứa một vài chữ cái (ví dụ: C, F, N, U).Swift - cách tạo phần chính xácForForectionIndexTitle?

Tôi muốn sửa đổi sectionForSectionIndexTitle để hiển thị đúng phần khi trượt ngón tay trên phần chỉ mục. Tôi vẫn không chắc chắn cách thực hiện bằng cách sử dụng sections.append ...

Cảm ơn bạn đã trợ giúp.

Đây là cách mã của tôi trông giống như:

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

nơi bạn gọi updateSections()? – DeyaEldeen

+0

Trên nhiều nơi (ví dụ: điều khiển phân đoạn hoặc UISwipeGestureRecognizerDirection). Đoạn mã trên chỉ là phần liên quan nhất liên quan đến câu hỏi. – Heky

+0

câu hỏi của bạn là gì? tôi có thể giúp gì? –

Trả lời

1

thử đoạn mã sau vào dự án của bạn:

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 
} 
Các vấn đề liên quan