三、使用自定义view类
在有table view的view controller类中实现UITableViewDelegate,UITableViewDataSource两个协议。
//
// PNTViewController.h
// SimpleTableStoryBoradTest
//
// Created by wong linwei on 12-9-5.
// Copyright (c) 2012年 P&T. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface PNTViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
NSArray *tableData;
}
@end
//
// PNTViewController.m
// SimpleTableStoryBoradTest
//
// Created by wong linwei on 12-9-5.
// Copyright (c) 2012年 P&T. All rights reserved.
//
#import "PNTViewController.h"
#import "SimpleTableViewCell.h"
@implementation PNTViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableData = [NSArray arrayWithObjects:@"first",@"second",@"third",@"forth",@"fifth",@"sixth", nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"SimpleTableViewCell";
SimpleTableViewCell *cell = (SimpleTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];//复用cell
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];//加载自定义cell的xib文件
cell = [array objectAtIndex:0];
}
cell.cellName.text = [tableData objectAtIndex:indexPath.row];
cell.cellTime.text = [NSString stringWithFormat:@"%@",[NSDate date]];
cell.cellImage.image = [UIImage imageNamed:@"titlepic.png"];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];//返回有多少项数据要显示
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;//此处返回cell的高度
}
@end
看下最后的结果吧:

