美文网首页IOS开发OC-开发案例收集
iOS开发自定义UITableView索引

iOS开发自定义UITableView索引

作者: dwoe | 来源:发表于2018-03-19 10:02 被阅读1695次

使用SectionIndexView自定义UITableView索引,项目的demo里面提供了全面的演示。

github地址

效果预览:

image image image image image

使用方式与使用UITableView的方式极其类似

1.首先创建SectionIndexView


override func viewDidLoad() {

    indexView = SectionIndexView.init(frame:frame)

    indexView.dataSource = self

    indexView.delegate = self

    view.addSubview(indexView)

    indexView.loadData()    // 需要手动调用一次loadData() 方法

}

2.实现代理方法SectionIndexViewDataSource


//SectionIndexView需要展示多少个数据

func numberOfItemViews(in sectionIndexView: SectionIndexView) -> Int {

    return indexData.count

}

//返回SectionIndexView的itemView, (类似于UITableViewCell)

//每个itemView的高度会平分SectionIndexView的高度

func sectionIndexView(_ sectionIndexView: SectionIndexView, itemViewAt section: Int) -> SectionIndexViewItem {

    let itemView = SectionIndexViewItem.init()

    itemView.title = indexData[section]

    return itemView

}

//当点击SectionIndexViewItem时,展示的预览图 SectionIndexViewItemPreview

//若不需要预览图,则无需实现

func sectionIndexView(_ sectionIndexView: SectionIndexView, itemPreviewFor section: Int) -> SectionIndexViewItemPreview {

    let preview = SectionIndexViewItemPreview.init(title: indexData[section], type: .default)

    return preview

}

3.根据需要实现代理方法SectionIndexViewDelegate


//当点击SectionIndexViewItem后的回调

func sectionIndexView(_ sectionIndexView: SectionIndexView, didSelect section: Int)

//当在SectionIndexView上移动时的回调

func sectionIndexView(_ sectionIndexView: SectionIndexView, toucheMoved section: Int)

//移动取消的回调

func sectionIndexView(_ sectionIndexView: SectionIndexView, toucheCancelled section: Int)

相关文章

网友评论

    本文标题:iOS开发自定义UITableView索引

    本文链接:https://www.haomeiwen.com/subject/klyuqftx.html