iphone实现一个最简单的TableView
@interface SimpleTableViewViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {
??然后加入成员变量m_data,是TableView待会要显示的数据。
?
?? ?代码如下:
@interface SimpleTableViewViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {NSArray *m_data;}@property (nonatomic, retain) NSArray *m_data;@end
?7. 单击?SimpleTableViewViewController.m 文件
?
?? ?在?@implementation?SimpleTableViewViewController 下面加如下一行代码
?
@synthesize m_data;
?8. 把?SimpleTableViewViewController.m 文件中的?viewDidLoad 函数的注释去掉并加入以下代码
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad {NSArray *arr = [[NSArray alloc] initWithObjects: @"桔子", @"雪梨", @"毛桃", @"李子", @"荔枝", @"柚子", @"芒果", @"菠萝", @"草莓", @"西瓜", nil];self.m_data = arr;[arr release]; [super viewDidLoad];}
?9. 最后在?SimpleTableViewViewController.m 文件最下面 @end 之前,加入以下代码
#pragma mark -#pragma mark Table View Data Source Methods- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return [m_data count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *TableViewDynamicLoadIdentifier = @"TableViewDynamicLoadIdentifier";UITableViewCell *pCell = [tableView dequeueReusableCellWithIdentifier:TableViewDynamicLoadIdentifier];if (pCell == nil){pCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewDynamicLoadIdentifier] autorelease];}NSInteger nRow = [indexPath row];pCell.textLabel.text = [m_data objectAtIndex:nRow];return pCell;}
?10. 编译并运行后的效果如下图如示:
?