需要实现的协议:
UITableViewDelegate
UITableViewDataSource
数据源:
// // DataSet.m // TableViewDemo // // Created by zhangqiang on 14-5-17. // Copyright (c) 2014年 zhangqiang. All rights reserved. // #import "DataSet.h" @implementation DataSet - (id) initWithDataSet { array=[NSArray arrayWithObjects:[[MeEntry alloc] initWithMeEntry:@"title1" subtitle:@"subtitle1" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/0" ofType:@"png"]] contents:@"contents1"], [[MeEntry alloc] initWithMeEntry:@"title2" subtitle:@"subtitle1" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/0" ofType:@"png"]] contents:@"contents2"] ,[[MeEntry alloc] initWithMeEntry:@"title3" subtitle:@"subtitle2" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/1" ofType:@"png"]] contents:@"contents3"] ,[[MeEntry alloc] initWithMeEntry:@"title4" subtitle:@"subtitle3" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/2" ofType:@"png"]] contents:@"contents4"] ,[[MeEntry alloc] initWithMeEntry:@"title5" subtitle:@"subtitle4" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/3" ofType:@"png"]] contents:@"contents5"] ,[[MeEntry alloc] initWithMeEntry:@"title6" subtitle:@"subtitle5" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/4" ofType:@"png"]] contents:@"contents6"] ,[[MeEntry alloc] initWithMeEntry:@"title7" subtitle:@"subtitle6" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/5" ofType:@"png"]] contents:@"contents7"] ,[[MeEntry alloc] initWithMeEntry:@"title8" subtitle:@"subtitle7" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/6" ofType:@"png"]] contents:@"contents8"] ,[[MeEntry alloc] initWithMeEntry:@"title9" subtitle:@"subtitle8" headimage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"resource/7" ofType:@"png"]] contents:@"contents9"], nil]; return self; } - (NSArray*) GetData { return array; } @end
实体类:
// // MeEntry.m // TableViewDemo // // Created by zhangqiang on 14-5-17. // Copyright (c) 2014年 zhangqiang. All rights reserved. // #import "MeEntry.h" @implementation MeEntry @synthesize title,subtitle,headImage,contents; - (id) initWithMeEntry:(NSString *)title subtitle:(NSString *)subtitle headimage:(UIImage *)headImage contents:(NSString *)contents { self.title=[NSString stringWithString:title]; self.subtitle=[NSString stringWithString:subtitle]; self.headImage=headImage; self.contents=[NSString stringWithString:contents]; return self; } @end
UITableViewDelegate 协议实现:
// // ViewController.m // TableViewDemo // // Created by zhangqiang on 14-5-17. // Copyright (c) 2014年 zhangqiang. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { ds=[[DataSet alloc] initWithDataSet]; index=0; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[ds GetData] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *data=[ds GetData]; UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"MyCell"]; if(cell==nil) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"]; } MeEntry * me=(MeEntry*)data[index]; cell.imageView.image=me.headImage; cell.textLabel.text=me.title; cell.detailTextLabel.text=me.contents; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; index++; if([data count]-index==1) { index=0; } return cell; } - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *data=[ds GetData]; MeEntry * me=(MeEntry*)data[indexPath.row]; cell.textLabel.text= me.title; NSLog(@"willDisplayCell: %@",cell.textLabel.text); } - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *data=[ds GetData]; MeEntry * me=(MeEntry*)data[indexPath.row]; NSString *msg=[NSString stringWithFormat:@"当前选择标题:%@",me.title]; UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertview show]; NSLog(@"didSelectRowAtIndexPath"); } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { } - (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"didDeselectRowAtIndexPath"); } @end