2012年12月27 我的第一个iphone小程序
先上效果图:
[img]
[/img]
工程结构图:
[img]
[/img]
上代码:
MyTabViewViewController.h
//// MyTabViewViewController.h// MyTabView//// Created by 张 志亮 on 12-12-27.// Copyright (c) 2012年 张 志亮. All rights reserved.//#import <UIKit/UIKit.h>@interface MyTabViewViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{IBOutlet UITableView *table; NSMutableArray *list1; NSMutableArray *list2;}@property(nonatomic,retain) IBOutlet UITableView *table;@property(nonatomic,retain) NSMutableArray *list1;@property(nonatomic,retain) NSMutableArray *list2;@end
//// MyTabViewViewController.m// MyTabView//// Created by 张 志亮 on 12-12-27.// Copyright (c) 2012年 张 志亮. All rights reserved.//#import "MyTabViewViewController.h"@interface MyTabViewViewController ()@end@implementation MyTabViewViewController@synthesize table;@synthesize list1;@synthesize list2;- (void)viewDidLoad{ //模拟添加数据 list1 = [[NSMutableArray alloc] init]; [list1 addObject:@"one"]; [list1 addObject:@"two"]; [list1 addObject:@"three"]; [list1 addObject:@"four"]; list2 = [[NSMutableArray alloc] init]; [list2 addObject:@"11111"]; [list2 addObject:@"22222"]; [list2 addObject:@"33333"]; [list2 addObject:@"44444"]; [list2 addObject:@"55555"]; [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}//- (NSInteger) numberOfSectionsInTableView : (UITableView*)tableView { return 2;}//- (NSInteger) tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section { if(section==0){ return [list1 count]; }else{ return [list2 count]; } }//- (NSString*) tableView: (UITableView*)tableView titleForHeaderInSection: (NSInteger)section { if(section==0){ return @"Food List-----One"; }else{ return @"Food List-----Two"; } }//- (UITableViewCell*) tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath { // NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSInteger number = indexPath.section; static NSString * identifier = @"identifier"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if( cell == nil ) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; if(number==0){ cell.text = [list1 objectAtIndex:row]; }else{ cell.text = [list2 objectAtIndex:row]; } return cell;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end