iOS之UITableViewController的使用

本文用UITableViewController实现上一篇的功能

创建空的iOS工程,不需要xib和storyboard文件,删除info.plist的相关文件

1、AppDelegate.h

#import <UIKit/UIKit.h>

@interface FKAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

2、FKViewController.h

#import <UIKit/UIKit.h>

@interface FKViewController : UITableViewController

@end

3、FKViewController.m

#import "FKViewController.h"
#import <QuartzCore/QuartzCore.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.tableView.tableHeaderView = [[UIImageView alloc]
		initWithImage:[UIImage imageNamed:@"tableheader.png"]];
}
// 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协议中的方法,该方法的返回值决定指定分区的页眉
- (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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值