UITableView右侧索引效果(分组圆角)

315 阅读1分钟

控制器

extension SportsFilterViewController {

    

    func firstLetterSortedArray() {
    
        // 二维数组
        var sectionArray = Array<Array<Any>>()
        
        let models = SportsFilterModel.testmodels()
        
        /// 索引集合

        let collation = UILocalizedIndexedCollation.current()

        

        // 这个对象中包含26个大写字母A-Z 和 #

        titles = collation.sectionTitles

        // 1、初始化二维数组
        for _ in titles {

            sectionArray.append(Array<Any>())

        }

        

        for model in models {

            // 2、根据 '@selector中的方法名' 返回的字符串的拼音首字母,找到这个首字母对应的下标index

            let section = collation.section(for: model, collationStringSelector: NSSelectorFromString("name"))

            // 根据index取出二维数组中的一维数组数

            sectionArray[section].append(model)

            //var subArray = secionArray[section]

            //subArray.append(model)

        }

        

        for var (i, subArray) in sectionArray.enumerated() {
            
            // 对各分组按指定(方法name)进行排序
            let sortArray = collation.sortedArray(from: subArray, collationStringSelector: NSSelectorFromString("name"))

            subArray.removeAll()

            subArray = sortArray

            debugPrint("subArray = \(subArray.count)")
            
            
            // 处理空元素数组
            if subArray.count == 0 {

                let emptyTitle = titles[i]

                emptyTitles.append(emptyTitle)

            } else if let model = subArray.first as? SportsFilterModel {

                // 最后一个单元格
                model.isLastIndex = true

                subArray[0] = model

            }

        }

        

        // 移除空数组

        for title in emptyTitles {

            if let index = titles.firstIndex(of: title) {

                titles.remove(at: index)

            }

        }

        sectionArray = sectionArray.filter{ !($0.isEmpty) }
        
        sectionModels = sectionArray

    }

    

    func addIndexBar() {

        let indexBar = IndexBar.init(frame: CGRect(x: view.bounds.width - 20.0, y: 0, width: 20.0, height: view.bounds.height - kTopBarHeight))

        indexBar.configure = { configure in

            configure.sectionWH = 18.0

            configure.titleFont = UIFont.systemFont(ofSize: 12)

            configure.backgroundColorForSelected = kThemeRedColor!

            configure.bubbleConfigure.backgroundColor = kThemeRedColor!

        }

        indexBar.setData(titles, tableView: groupedTableView)

        view.addSubview(indexBar)

    }
    
}

Header头部视图圆角

extension SportsFilterHeaderFooterView {

    private func initSubViews() {
        self.addSubview(containerView)
        containerView.addSubview(leftLineView)
        containerView.addSubview(titleLabel)
    }

    private func addConstants() {
        containerView.snp.makeConstraints { make in
            make.top.equalTo(self).offset(10)
            make.bottom.equalTo(self)
            make.left.right.equalTo(self).inset(10)
        }
    }
    
}

section最后一个单元格圆角

class SportsFilterCell: UITableViewCell {

    
    override func layoutSubviews() {

        super.layoutSubviews()

        if let model = sportsFilterModel, model.isLastIndex == true {

            containerView.addCorner(roundingCorners: [UIRectCorner.bottomLeft, UIRectCorner.bottomRight], cornerSize: CGSize(width: 10.0, height: 10.0))

        } else {

            containerView.addCorner(roundingCorners: [UIRectCorner.bottomLeft, UIRectCorner.bottomRight], cornerSize: CGSize.zero)

        }

    }

}

效果图:

Simulator Screen Shot - iPhone 11 - 2022-04-20 at 23.24.56.png