iPhone開發之UITableView簡單應用(5) – iPhone手機開發技術文章 iPhone軟體開發教學課程

  1.單擊菜單File->New->Proget…..新建一個項目:

   

   2.選擇Single View Application模板,單擊Next:

   

   3.將項目命名為Simple Table,單擊Next->Create,新建項目:

   

   4.單擊ViewController.xib文件,在View視圖上添加控件Table View:

   

  5.為控件Table View添加數據、方法委托:

   

 

  6.單擊ViewController.h頭文件,為ViewController添加數據成員:
[plain]
#import <UIKit/UIKit.h> 
 
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> 

   NSArray *list; 

@property(nonatomic,retain)NSArray *list; 
 
@end 

  7.單擊ViewController.m文件,為ViewController添加數據和協議方法:
[plain]
#import "ViewController.h" 
 
@interface ViewController () 
 
@end 
 
@implementation ViewController 
@synthesize list; 
 
– (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSArray *array = [[NSArray alloc]initWithObjects:@"廣東",@"湖南",@"北京",@"上海",@"香港",@"澳門", nil]; 
    self.list = array; 
    [array release]; 

 
– (void)viewDidUnload 

    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    self.list = nil; 

 
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
-(void)dealloc 

    [super dealloc]; 
    [list release]; 

 
#pragma mark – 
#pragma mark Table Data Source Methods 
 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    return [list count]; 

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

    static NSString *identfier=@"placetable"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier]; 
    if(cell==nil) 
    { 
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfier]; 
    } 
    NSInteger row = [indexPath row]; 
    cell.textLabel.text = [list objectAtIndex:row]; 
    return cell; 

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

    NSInteger row = [indexPath row]; 
    NSString *message = [list objectAtIndex:row]; 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"你選擇:" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 

 
@end 
  8.現在可以單擊運行按鈕,測試程序,單擊其中的選項“廣東”,效果如下:
  

  6.單擊ViewController.h頭文件,為ViewController添加數據成員:
[plain]
#import <UIKit/UIKit.h> 
 
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> 

   NSArray *list; 

@property(nonatomic,retain)NSArray *list; 
 
@end 

  7.單擊ViewController.m文件,為ViewController添加數據和協議方法:
[plain]
#import "ViewController.h" 
 
@interface ViewController () 
 
@end 
 
@implementation ViewController 
@synthesize list; 
 
– (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSArray *array = [[NSArray alloc]initWithObjects:@"廣東",@"湖南",@"北京",@"上海",@"香港",@"澳門", nil]; 
    self.list = array; 
    [array release]; 

 
– (void)viewDidUnload 

    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    self.list = nil; 

 
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
-(void)dealloc 

    [super dealloc]; 
    [list release]; 

 
#pragma mark – 
#pragma mark Table Data Source Methods 
 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    return [list count]; 

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

    static NSString *identfier=@"placetable"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier]; 
    if(cell==nil) 
    { 
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfier]; 
    } 
    NSInteger row = [indexPath row]; 
    cell.textLabel.text = [list objectAtIndex:row]; 
    return cell; 

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

    NSInteger row = [indexPath row]; 
    NSString *message = [list objectAtIndex:row]; 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"你選擇:" message:message delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 

 
@end 
  8.現在可以單擊運行按鈕,測試程序,單擊其中的選項“廣東”,效果如下:
  

作者:js_dada

發佈留言