在TableViewController中将排序数据管理到索引部分和单元格中

问题描述 投票:0回答:1

如何建立像iPhone的“联系人”列表一样的桌面视图?

我的原始数据在JSON中,下载并打包到名为Article.swift的Model Class对象中。 “文章”是它的要素。

article.name = rawData["A_Name_Ch"] as! String
article.name_EN = rawData["A_Name_En"] as! String
article.location = rawData["A_Location"] as! String
article.image_URLString = rawData["A_Pic01_URL"] as? String
........
........

和数据按article.sorted排序:

func downLoadLatestArticles(){
    Article.downLoadItem { (articles, error) in
        if let error = error {
            return
        }
        if let articles = articles {
            self.articles = articles.sorted(by: { $0.name_EN! < $1.name_EN! })
        }
    }
}

我们想使用article.name作为排序的关键,通过章节标题(A,B,C ..),滚动侧索引表A~Z显示关于tableview的文章信息,并引导到包含详细数据的下一个视图单击单元格时。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(searchActive) {
        return filtered.count
    }
    return articles.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell", for: indexPath) as! ListTableCell

    var article : Article

    let imageURL: URL?
    if let imageURLString = article.image_URLString {
        imageURL = URL (string: imageURLString)
    }
    else {  imageURL = nil   }

    if let c = cell as? ListTableCell {

        c.nameLabel?.text = article.name;
        c.name_ENLabel?.text = article.name_EN;
        c.locationLabel?.text = article.location
    }
    return cell
}
ios swift uitableview
1个回答
0
投票

准备好使用Array之后,可以使用此方法对数组元素进行排序。

articles = articles.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == ComparisonResult.orderedDescending }
© www.soinside.com 2019 - 2024. All rights reserved.