iOS.UIKit.15.UITableView -- Index

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

图01

2、team_dictionary.plist


<?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>

3、.h


#import <UIKit/UIKit.h>

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

@end

4、.m


#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

iOS.UIKit.15.UITableView -- Index

时间: 2024-08-19 02:00:26

iOS.UIKit.15.UITableView -- Index的相关文章

iOS.UIKit.17.UITableView -- Cells Operation

1.案例介绍:表视图中单元格的增加.删除.移动,如图01,02 图01图02 2..h #import <UIKit/UIKit.h> @interface CQ26ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> @property (weak,nonatomic) IBOutlet UINavigationItem *navgation

iOS.UIKit.14.UITableView -- UISearchBar

1.案例介绍:一个具备搜索功能的表视图,如图01,02,03 图01图02图03 2.Main.storyboard,如图04 图04 3..h #import <UIKit/UIKit.h> @interface CQ23ViewController : UITableViewController<UISearchBarDelegate, UISearchDisplayDelegate> // 搜索栏 @property (weak, nonatomic) IBOutlet UI

iOS.UIKit.13.UITableView -- Simple

1.案例介绍:最简单的表视图,如图01 图01 2.team.plist <?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="

iOS.UIKit.16.UITableView -- Static Cells

1.使用表视图的分组,静态单元格布局,如图01 图01 2.Main.storyboard

IOS开发中UITableView(表视图)的性能优化及自定义Cell

IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITableView实现的.UITableView这个控件中的列表的每一行是一个cell,当UITableView中cell数量特别大的时候,由于每次都需要alloc分配内存并初始化,会导致app运行不流畅,所以可以使用苹果提供的几个方法进行优化,我把这个过程记录下来供自己以后查阅. 当然,既然说到优化,那我们

IOS学习之UiTableView下拉刷新与自动加载更多,百年不变的效果

IOS学习之UiTableView下拉刷新与自动加载更多,百年不变的效果(五) 五一劳动节马上来临,小伙伴有妹有很激动哟,首先祝天下所有的程序猿节日快乐!这个五一对于我来说有点不一样,我的人生从这个五一就转弯了,爱情长跑8年的我结婚了,一会支付宝账号我会公布出去,请自觉打款!谢谢合作. 灯光闪起来: 舞蹈跳起来: 歌曲唱起来: -------------------------------------------------------------------------------------

IOS开发系列--UITableView使用全面解析

--UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不开它强大的功能,今天这篇文章将针对UITableView重点展开讨论.今天的主要内容包括: 基本介绍 数据源 代理 性能优化 UITableViewCell 常用操作 UITableViewController MVC模式 基本介绍 UITableVie

iOS开发系列--UITableView全面解析

概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不开它强大的功能,今天这篇文章将针对UITableView重点展开讨论.今天的主要内容包括: 基本介绍 数据源 代理 性能优化 UITableViewCell 常用操作 UITableViewController MVC模式 基本介绍 UITableView有两种风格:UITableViewSt

【UIKit】UITableView.04

UITableView.04: [1]拖入一个UITableView [2]将TableView的dataSource与控制器连接 [3]首先得遵循UITableView的数据源协议<UITableViewDataSource> [4]加入图标文件 [5]代码 1.创建一个Product类,用来作为对象内容表示产品信息 2.在Product.h中添加声明代码 @interface Product : NSObject /*********设置产品内容信息*********/ /* 图片*/ @