今天写一个简单的表视图Demo 完成界面如下图所示:
1. 下面开始新建一个项目
选择xcode项目中的Single View Application 创建一个新项目,名称为GPSimple_TableView。
2.今天要做的是一个简单的表视图Demo,所以要明确自己要做什么工作,先准备知识。
1)创建传递给表的数据的数组 在 - (void)viewDidLoad:方法中实现(数组可以来源于文本文件、属性列表或URL)。
数据源方法Table View Data Source Methods 中要要必须实现2个方法。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
这个方法是表来查看指定分区中有多少行。用于返回表分区中的行数,只需返回数组中数组项的数量即可。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
用于表视图绘制其中的一行时调用。
3.下面是具体实现步骤:
1)在GPViewController.h中添加数据源和委托,创建2个数组来存储数据和图片命名为litsData和listImage,如下所示。
#import <UIKit/UIKit.h>
@interface GPViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
@property (retain, nonatomic) NSArray *listImage;
@property (retain, nonatomic) NSArray *listData;
@end
在GPViewController.m文件中合成属性
@implementation GPViewController
@synthesize listData;
@synthesize listImage;
2)在- (void)viewDidLoad:方法中创建数组,添加代码
先导入图片文件夹
NSArray *array = [[NSArray alloc] initWithObjects:@"A1-南非",@"A2-墨西哥",
@"B1-阿根廷",@"B2-尼日利亚",@"C1-英格兰",@"C2-美国",
@"D1-德国",@"D2-澳大利亚",@"E1-荷兰",@"E2-丹麦",
@"G1-巴西",@"G2-朝鲜",@"H1-西班牙",@"H2-瑞士",nil];
NSArray *images = [[NSArray alloc] initWithObjects:@"SouthAfrica.png",@"Mexico.png",
@"Argentina.png",@"Nigeria.png",@"England.png",@"USA.png",
@"Germany.png",@"Australia.png",@"Holland.png",@"Denmark.png",
@"Brazil.png",@"NorthKorea.png",@"Spain.png",@"Switzerland.png",nil];
self.listData = array;
self.listImage = images;
并释放数组
- (void)viewDidUnload
{
[super viewDidUnload];
self.listData = nil;
self.listImage = nil;
}
3)实现数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleCellIdentifier = @"SimpleCellIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleCellIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
UIImage *img = [UIImage imageNamed:[listImage objectAtIndex:row]];
cell.imageView.image = img;
return cell;
}
运行下模拟器和第一副图一样
4)下面来实现委托方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:@"你选择了%@队。", rowValue];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"行选择"
message:message
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
运行:
发现一个问题:模拟器中先点击一行不会弹出警告框,再点击下一个就弹出警告框。
- (void)viewDidLoad: 添加属性列表的模式代码参考:
- (void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:@"足球队dictionary" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
self.teams = dict;
[dict release];
self.teamsname = [[teams allKeys] sortedArrayUsingSelector:@selector(compare:)];
}
3.补充
实现索引:分段代码基础上