ipad开发对表格视图的基本实现

来源:互联网 发布:抢购软件源代码 编辑:程序博客网 时间:2024/06/10 01:15


#import "RootViewController.h"

@implementation RootViewController

@synthesize contactInformationViewController;

//定义表格中的数据集合

NSMutableArray *listOfContacts;

//当窗口加载完成初始化数据集合的内容

- (void)viewDidLoad {

    [superviewDidLoad];

listOfContacts = [[NSMutableArrayalloc] init];

[listOfContactsaddObject:@"张三"];

[listOfContactsaddObject:@"李四"];

self.navigationItem.title =@"联系人";

[superviewDidLoad];

}

//只有一组列表显示如果显示,返回值为组数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

//关键方法,返回当前列表一共有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [listOfContactscount];

}

//定义表格中的每一行显示的内容,在这里假如nslog 就可以知道运行原理了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier =@"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];//获取内存中的条目,如果获取失败则创建该条目

    if (cell == nil) {

        cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] autorelease];

    }

    

cell.textLabel.font = [UIFontsystemFontOfSize:17];

NSString *cellValue = [listOfContactsobjectAtIndex:indexPath.row];

cell.textLabel.text = cellValue;


    return cell;

}



//实现选中某一行的事件处理

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSUInteger row = [indexPath row];

NSString *contactName = [listOfContactsobjectAtIndex:row];

if (self.contactInformationViewController ==nil) { //创建一共弹出窗口

ContactInformationViewController *c = [[ContactInformationViewControlleralloc]

 initWithNibName:@"ContactInformationView" 

 bundle:[NSBundle mainBundle]];

self.contactInformationViewController = c;

[c release];

}

[self.contactInformationViewControllerinitWithContactName:contactName];

//弹出窗口

[self.navigationControllerpushViewController:self.contactInformationViewController

animated:YES];

}


- (void)dealloc {

[listOfContactsrelease];

    [super dealloc];

}



@end


原创粉丝点击