码迷,mamicode.com
首页 > 其他好文 > 详细

【精】表格(UITableView)总结(2):索引(IndexList)

时间:2015-04-14 10:02:34      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:ios   swift   uitableview   索引   indexlist   

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/45037159

1、前言

当我们的 Table View 中数据量比较大、分段比较多的时候我们可以引入 IndexList 来方便地完成分段的定位,例如系统的通讯录程序。我们可以通过设置 Table View 的 sectionIndexMinimumDisplayRowCount 属性来指定当 Table View 中有多少行的时候开始显示 IndexList,默认的设置是 NSIntegerMax,即默认是不显示 IndexList 的。

为了能够使用 IndexList 我们还需要实现 UITableViewDataSource 中以下两个方法:

// 设置分段索引数组
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!

// 点击索引触发事件(一般不需要写,默认 section index 顺序与 section 对应)
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int

2、演示示例

技术分享

3、演示代码

//
//  ViewController.swift
//  UITableViewSample-IndexList
//
//  Created by jinnchang on 15/4/13.
//  Copyright (c) 2015年 Jinn Chang. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var tableView: UITableView!
    var data = ["C" : ["蔡依林", "蔡健雅"], "F" : ["范玮琪", "范晓萱", "方大同", "费玉清", "费翔", "付笛生"],
        "L" : ["梁静茹", "林忆莲", "李健", "林俊杰", "林宥嘉"], "T" : ["田馥甄", "谭维维"],
        "Z" : ["张靓颖", "张惠妹", "卓依婷", "周杰伦", "张杰", "张学友", "张国荣", "张信哲", "周传雄", "周华健", "张震岳", "张敬轩", "张宇"]]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        tableView = UITableView(frame: CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height), style: .Plain)
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.delegate = self
        tableView.dataSource = self
        // 设置索引颜色
        tableView.sectionIndexColor = UIColor.redColor()
        // 设置索引背景颜色
        tableView.sectionIndexBackgroundColor = UIColor.clearColor()
        
        self.view.addSubview(tableView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // 设置分段数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return data.count
    }
    
    // 设置每个分段标题(即索引内容)
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return data.keys.array[section]
    }
    
    // 设置分段索引数组
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
        return data.keys.array
    }
    
    // 点击索引触发事件(一般不需要写,默认 section index 顺序与 section 对应)
    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        let selectedIndexPath = NSIndexPath(forRow: 0, inSection: index)
        tableView.scrollToRowAtIndexPath(selectedIndexPath, atScrollPosition: .Bottom, animated: true)
        return index
    }
    
    // 设置每个分段对应的行数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[data.keys.array[section]]!.count
    }
    
    // 设置每行的具体内容
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        let sectionData = data[data.keys.array[indexPath.section]]
        cell.textLabel?.text = sectionData![indexPath.row]
        return cell
    }
    

}
Github上项目地址:https://github.com/jinnchang/SwiftSamples/tree/master/advanced/UITableViewSample-IndexList

4、结语

参考资料如下:

About Table Views in iOS Apps
文章最后更新时间:2015年4月14日09:02:50

【精】表格(UITableView)总结(2):索引(IndexList)

标签:ios   swift   uitableview   索引   indexlist   

原文地址:http://blog.csdn.net/jinnchang/article/details/45037159

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!