iOS之UITableView的使用——多分区表格以及分区索引

1、.h

#import <UIKit/UIKit.h>

@interface FKViewController : UIViewController<UITableViewDataSource
	, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *table;

@end

2、.m

#import <QuartzCore/QuartzCore.h>
#import "FKViewController.h"

@interface FKViewController ()

@end

@implementation FKViewController
NSDictionary* tableData;
NSArray* stories;
- (void)viewDidLoad
{
    [super viewDidLoad];
	tableData = [NSDictionary dictionaryWithObjectsAndKeys:
		[NSArray arrayWithObjects:@"孙悟空" , @"猪八戒", @"牛魔王"
			, @"蜘蛛精"  , @"唐僧" , @"沙和尚" , nil] , @"西游记",
		[NSArray arrayWithObjects:@"宝玉" , @"黛玉", @"元春"
	 		, @"探春"  , @"惜春" , @"可卿" , nil] , @"红楼梦",
		[NSArray arrayWithObjects:@"武松" , @"林冲", @"鲁达"
		 	, @"杨志"  , @"宋江" , @"史进" , nil] , @"水浒",
		[NSArray arrayWithObjects:@"关羽" , @"刘备", @"张飞"
		 , @"曹操"  , @"张辽" , @"吕布" , nil] , @"三国演义", nil];
	// 获取tableData的所有key排序后组成的数组
	stories = [[tableData allKeys]
		sortedArrayUsingSelector:@selector(compare:)];
	self.table.dataSource = self;
	self.table.delegate = self;
}
// UITableViewDataSource协议中的方法,该方法的返回值决定表格包含多少个分区
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
	// stories集合包含多少个元素,就包含多少个分区
	return stories.count;
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区包含多少个元素
- (NSInteger)tableView:(UITableView *)tableView
 	numberOfRowsInSection:(NSInteger)section
{
	// 获取指定分区对应stories集合中的元素
	NSString* story = [stories objectAtIndex:section];
	// 该stories集合元素包含多少个人物,该分区就包含多少表格行
	return [[tableData objectForKey:story] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
	cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	// 获取分区号
	NSUInteger sectionNo = indexPath.section;
	// 获取表格行的行号
	NSUInteger rowNo = indexPath.row;
	NSString* story = [stories objectAtIndex:sectionNo];
	static NSString* cellId = @"cellId";
	UITableViewCell* cell = [tableView
		dequeueReusableCellWithIdentifier:cellId];
	if(cell == nil)
	{
		cell = [[UITableViewCell alloc] initWithStyle:
			UITableViewCellStyleDefault reuseIdentifier:cellId];
	}
	// 将单元格的边框设置为圆角
	cell.layer.cornerRadius = 12;
	cell.layer.masksToBounds = YES;
	// 为表格行的textLabel设置文本
	cell.textLabel.text = [[tableData objectForKey:story]
		objectAtIndex:rowNo];
	return cell;
}
// UITableViewDataSource协议中的方法,该方法的返回值用于在表格右边建立一列浮动的索引。
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
	return stories;
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的页眉
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection
		:(NSInteger)section
{
	return [stories objectAtIndex:section];
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的页脚
- (NSString*) tableView:(UITableView *)tableView titleForFooterInSection
		:(NSInteger)section
{
	NSString* story = [stories objectAtIndex:section];	
	return [NSString stringWithFormat:@"一共有%d个人物"
		, [[tableData objectForKey:story] count]];
}
@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值