码迷,mamicode.com
首页 > 移动开发 > 详细

iOS.UIKit.15.UITableView -- Index

时间:2014-06-06 15:06:07      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

1、案例介绍:具备索引功能的分节表视图,如图01

bubuko.com,布布扣图01

2、team_dictionary.plist

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>A组</key>
    <array>
        <string>A1-南非</string>
        <string>A2-墨西哥</string>
        <string>A3-乌拉圭</string>
        <string>A4-法国</string>
    </array>
    <key>B组</key>
    <array>
        <string>B1-阿根廷</string>
        <string>B2-尼日利亚</string>
        <string>B3-韩国</string>
        <string>B4-希腊</string>
    </array>
    <key>C组</key>
    <array>
        <string>C1-英格兰</string>
        <string>C2-美国</string>
        <string>C3-阿尔及利亚</string>
        <string>C4-斯洛文尼亚</string>
    </array>
    <key>D组</key>
    <array>
        <string>D1-德国</string>
        <string>D2-澳大利亚</string>
        <string>D3-塞尔维亚</string>
        <string>D4-加纳</string>
    </array>
    <key>E组</key>
    <array>
        <string>E1-荷兰</string>
        <string>E2-丹麦</string>
        <string>E3-日本</string>
        <string>E4-喀麦隆</string>
    </array>
    <key>F组</key>
    <array>
        <string>F1-意大利</string>
        <string>F2-巴拉圭</string>
        <string>F3-斯洛伐克</string>
        <string>F4-新西兰</string>
    </array>
    <key>G组</key>
    <array>
        <string>G1-巴西</string>
        <string>G2-朝鲜</string>
        <string>G3-科特迪瓦</string>
        <string>G4-葡萄牙</string>
    </array>
    <key>H组</key>
    <array>
        <string>H1-西班牙</string>
        <string>H2-瑞士</string>
        <string>H3-洪都拉斯</string>
        <string>H4-智利</string>
    </array>
</dict>
</plist>
bubuko.com,布布扣

3、.h

bubuko.com,布布扣
#import <UIKit/UIKit.h>

@interface CQ24ViewController : UITableViewController<UISearchBarDelegate>
// 表数据
@property (strong,nonatomic) NSDictionary *dictData;
// 表数据分组名集合
@property (strong,nonatomic) NSArray *listGroupname;

@end
bubuko.com,布布扣

4、.m

 

bubuko.com,布布扣
#import "CQ24ViewController.h"

@interface CQ24ViewController ()

@end

@implementation CQ24ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *plistPath = [mainBundle pathForResource:@"team_dictionary" ofType:@"plist"];
    
    // 1、初始化表格数据dictData
    self.dictData = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    // 2、初始化分组名称,排序
    NSArray *tempList = [self.dictData allKeys];
    self.listGroupname = [tempList sortedArrayUsingSelector:@selector(compare:)];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark 返回分组数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 1、获取分组名称
    NSString *groupName = [self.listGroupname objectAtIndex:section];
    // 2、将分组名称作为key,从字典中取出分组数据
    NSArray *listTeams = [self.dictData objectForKey:groupName];
    return [listTeams count];
    
}
#pragma mark 填充每个节的Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1、加载一个cell
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // 2、获取选择的节
    NSUInteger section = [indexPath section];
    // 3、获取节名称
    NSString *groupName = [self.listGroupname objectAtIndex:section];
    // 4、获取节数据
    NSArray *listTeams = [self.dictData objectForKey:groupName];
    // 5、获取选中节中的行索引
    NSUInteger row = [indexPath row];
    // 6、设置cell中的text
    cell.textLabel.text = [listTeams objectAtIndex:row];
    return cell;
}
#pragma mark 节数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.listGroupname.count;
}
#pragma mark 设置节title
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *groupName = [self.listGroupname objectAtIndex:section];
    return groupName;
}
#pragma mark 索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    // 1、存在索引的集合
    NSMutableArray *listTitles = [[NSMutableArray alloc] initWithCapacity:[self.listGroupname count]];
    // 2、初始化索引集合
    for (NSString *item in self.listGroupname) {
        NSString *title = [item substringToIndex:1];
        [listTitles addObject:title];
    }
    return listTitles;
}

@end
bubuko.com,布布扣

 

iOS.UIKit.15.UITableView -- Index,布布扣,bubuko.com

iOS.UIKit.15.UITableView -- Index

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/cqchen/p/3766600.html

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