如何在UITableview中设置字母顺序部分

时间:2015-12-05 20:35:03

标签: ios swift uitableview

我在这一点上陷入困​​境,我想在其中设置带有正确单元格的字母部分。到目前为止,我已经能够将“a”中的部分添加到“z”,但我的所有单元格都列在所有部分下面。我无法想出一个对正确部分下的细胞进行分类的方法。希望你们能帮忙 - 谢谢。

到目前为止,这是我的代码:

var PersonName: [String?]? = []
var PersonNumber : [Array<String?>?]? = []
var Dic:[String:String?]? = [String:String?]?()

let sections:Array<String> = ["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]? {
        return self.sections
    }

    func tableView(tableView: UITableView,
        sectionForSectionIndexTitle title: String,
        atIndex index: Int) -> Int{

            return index
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int{

        return sections.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (PersonName?.count)!
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell:UITableViewCell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell")

        cell.textLabel?.text = PersonName![indexPath.row]!
        cell.detailTextLabel?.text = PersonNumber![indexPath.row]!.first!

        return cell

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let indexPath = tableView.indexPathForSelectedRow!

        let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell

    }


    func tableView(tableView: UITableView,
        titleForHeaderInSection section: Int) -> String?{

            return self.sections[section]
    }

2 个答案:

答案 0 :(得分:0)

您需要什么以下是格式化您的数据,如下所示

你需要数组包含带字符串的字典作为字母表显示为截面和值作为数组或可变数组显示为行

( { a : ( {(apple), (air) , (ant) } ) } { b : ( {(ball), (bat) , (boost) } ) } )

希望有所帮助

答案 1 :(得分:0)

您需要创建“ Array of Array”以实现此目的。签出代码 下面这样做。

  var nameList = ["Abc","Bbc","Cbc",......]//whatever it is
  var arrNameList = [[nameList]]()
  var str = String()

  override func viewDidLoad() {

    str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" //Sections to make

    for i : Int in 0 ..< (str.count) { //loop to get section
        var arr = [nameList]() //Create temp array
        for j : Int in 0 ..< nameList.count { //loop to get name
            let name = nameList[j]
            let index = str.index(str.startIndex, offsetBy: i)
            let char = str[index]
            if name.first == char { 
                arr.append(name)
            }
        }
        arrNameList.append(arr) 
     }
  }

  func numberOfSections(in tableView: UITableView) -> Int {
    return arrNameList.count
  }

  func tableView(_ tableView: UITableView, titleForHeaderInSection 
  section: Int) -> String? {
    let index = str.index(str.startIndex, offsetBy: section)
    let char = str[index]
    return char
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection 
  section: Int) -> Int {
    return arrNameList[section].count
  }

  func tableView(_ tableView: UITableView, heightForHeaderInSection 
  section: Int) -> CGFloat {
    return 40.0
  }

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: 
  IndexPath) -> CGFloat {
    return 40.0
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
  IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", 
    for: indexPath) as! NameListTableViewCell

    let name = arrNameList[indexPath.section][indexPath.row]
    cell.nameLbl.text = name

    cell.selectionStyle = .none
    return cell
}

希望有帮助。