swift-TableView grouped by the first capital letter of Chinese

Ideas

To achieve the effect of grouping contacts by first letter similar to Apple's own address book, the idea of ​​using UITabView is roughly as follows:

  1. Convert name to pinyin
  2. Capitalize first letter of pinyin
  3. Take the first letter, remove duplicates + sort

such as:

let contacts : [String ] = [张三,张全蛋,李四,王五,people,王尼玛]

get

[Z,L,W,P]

achieve

  1. Entity class, add an initial field
class FriendUserModel {
    
    
    var userInfo: CIM_Def_CIMUserInfo!
    var nameFirstCharacter: Character! // 昵称首字母

    init(user: CIM_Def_CIMUserInfo) {
    
    
        userInfo = user

        // 汉字转成拼音
        let str = NSMutableString(string: user.nickName) as CFMutableString
        CFStringTransform(str, nil, kCFStringTransformToLatin, false)
        
        // 拼音去掉拼音的音标
        CFStringTransform(str, nil, kCFStringTransformStripDiacritics, false)
        
        // 大写字母
        var s: String = String(str)
        s = s.capitalized // 大写首字母
        nameFirstCharacter = s[s.startIndex]
    }
}

The above code, by injecting a basic user information (including user ID, nickname, etc.), and then automatically calculate the capital pinyin first letter.

  1. Cyclic data source, de-duplication calculation group
class IMFriendViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    @IBOutlet var userTabview: UITableView!

    var userList: [FriendUserModel] = []   // 数据源
    var group: [Character] = []            // 首字母去重后的分组

    override func viewDidLoad() {
    
    
        // 这里省略userList从服务器拉取加载的代码
        //for item in rsp.userInfoList {
    
    
        //    let model = FriendUserModel(user: item)
        //    self.userList.append(model)
        //}

		// 计算首字母分组
        for item in self.userList {
    
    
            // 去重判断
            if self.group.firstIndex(of: item.nameFirstCharacter) == nil {
    
    
                self.group.append(item.nameFirstCharacter)
            }
        }

        // 排序
        self.group = self.group.sorted(by: {
    
     (caracter0, character1) -> Bool in
            return caracter0 < character1
        })
    }
  1. TableViewDataSource agent handles loading logic
extension IMFriendViewController {
    
    
    // 几组?
    func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
    
    
        return group.count
    }

    // 每组多少人?
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    
        return userList.filter {
    
     (emp) -> Bool in emp.nameFirstCharacter == group[section] }.count
    }

    // 组名?
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    
    
        if group.count > 0 {
    
    
            return String(group[section])
        }
        return nil
    }

    // 如果不需要分组,直接返回列表的大小
//    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    
//        return userList.count
//    }

    // 返回一个TabViewCell实例,TabViewCell就是一行数据,真正用来显示的
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
        // cellId是类名
        let classType = IMFriendViewCell.self
        let cellId = String(describing: classType)

		// 找到这一组要显示的用户
        let showUsers: [FriendUserModel] = userList.filter {
    
     (emp) -> Bool in
            emp.nameFirstCharacter == group[indexPath.section]
        }

        // 获取一个可重复使用的Cell,没有就收到创建
        var cell = tableView.dequeueReusableCell(withIdentifier: cellId) as? IMFriendViewCell
        if cell == nil {
    
    
            cell = IMFriendViewCell(style: .default, reuseIdentifier: cellId)
        }

        // 渲染一行
        cell!.setContent(user: showUsers[indexPath.row].userInfo)
        return cell!
    }
}

Reference: How to group
TableView by the first capital letter of Chinese

effect

Show results

Guess you like

Origin blog.csdn.net/xmcy001122/article/details/109388367