iPhone UITableView 使用方法(一) – iPhone手機開發技術文章 iPhone軟體開發教學課程

UITableView是iPhone中比較常用的,用的比較多的控件,下面我們使用UITableView創建一個簡單的表格,效果如下:

 

如果要表格中增加數據的話,需要增加UITableViewDataSource協議。

如果需要響應用戶單擊的話,需要增加UITableViewDelegate協議。

 

1、創建項目:使用模板Single View Application新建一個項目,僅支持iPhone。

2、在ViewController.h中增加UITableViewDataSource和UITableViewDelegate協議,如下:

[cpp]
#import <UIKit/UIKit.h>  
 
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> { 
 
    NSArray * listData; 

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

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {

    NSArray * listData;
}

@property ( nonatomic, retain) NSArray *listData;

@end

3、往列表中增加數據,實現UITableViewDataSource協議,如下:

[cpp] view plaincopyprint?//返回總行數  
-(NSInteger ) tableView:(UITableView *)tableView 
 
  numberOfRowsInSection:(NSInteger )section 
 
{     
    return [ self.listData count ]; 
     

 
// 添加每一行的信息  
– (UITableViewCell *) tableView:(UITableView *)tableView 
          cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 
{    
     
    NSString *tag=@"tag"; 
     
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag]; 
     
    if (cell==nil ) { 
        cell=[[[ UITableViewCell alloc ] initWithFrame : CGRectZero 
                                        reuseIdentifier:tag] autorelease]; 
    }     
     
    NSUInteger row=[indexPath row]; 
     
    //設置文本  
    cell.text =[listData objectAtIndex :row]; 
     
    //選中後的顏色又不發生改變,進行下面的設置  
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;   
     
    //不需要分割線  
    //tableView.separatorStyle=UITableViewCellSeparatorStyleNone;    
     
    return cell; 
     

//返回總行數
-(NSInteger ) tableView:(UITableView *)tableView

  numberOfRowsInSection:(NSInteger )section

{   
    return [ self.listData count ];
   
}

// 添加每一行的信息
– (UITableViewCell *) tableView:(UITableView *)tableView
          cellForRowAtIndexPath:(NSIndexPath *)indexPath

{  
   
    NSString *tag=@"tag";
   
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];
   
    if (cell==nil ) {
        cell=[[[ UITableViewCell alloc ] initWithFrame : CGRectZero
                                        reuseIdentifier:tag] autorelease];
    }   
   
    NSUInteger row=[indexPath row];
   
    //設置文本
    cell.text =[listData objectAtIndex :row];
   
    //選中後的顏色又不發生改變,進行下面的設置
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    //不需要分割線
    //tableView.separatorStyle=UITableViewCellSeparatorStyleNone; 
   
    return cell;
   
}

4、響應用戶單擊事件,實現UITableViewDelegate協議,如下:

[cpp]
//響應用戶單擊事件  
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
     
    UIAlertView* showSelection; 
    NSString* message; 
     
    message = [[NSString alloc]initWithFormat:@"You chose the : %@", 
                     [self.listData objectAtIndex:indexPath.row]]; 
     
    showSelection = [[UIAlertView alloc] 
                     initWithTitle:@"Selected" 
                     message:message 
                     delegate:nil 
                     cancelButtonTitle:@"OK" 
                     otherButtonTitles:nil];    
     
    [showSelection autorelease]; 
    [showSelection show]; 

//響應用戶單擊事件
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    UIAlertView* showSelection;
    NSString* message;
   
    message = [[NSString alloc]initWithFormat:@"You chose the : %@",
                     [self.listData objectAtIndex:indexPath.row]];
   
    showSelection = [[UIAlertView alloc]
                     initWithTitle:@"Selected"
                     message:message
                     delegate:nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];  
   
    [showSelection autorelease];
    [showSelection show];
}

5、往ViewController中增加UITableView,並將UITableView的delegate和dataSource連接到ViewController。如下圖所示:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6、完整的代碼如下:

[cpp]
#import "ViewController.h"  
 
@interface ViewController () 
 
@end 
 
@implementation ViewController 
 
@synthesize listData; 
 
– (void)viewDidLoad 

     
    self.listData =[[NSArray alloc] initWithObjects:@"Item1",@"Item2",@"Item3", @"Item4", @"Item5", @"Item6", @"Item7",nil];; 
 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib.  

 
– (void)viewDidUnload 

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

 
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
 
#pragma mark – Table view data source delegate  
 
//返回總行數  
-(NSInteger ) tableView:(UITableView *)tableView 
  numberOfRowsInSection:(NSInteger )section 
{     
    return [ self.listData count ]; 

 
// 添加每一行的信息  
– (UITableViewCell *) tableView:(UITableView *)tableView 
          cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 
{    
     
    NSString *tag=@"tag"; 
     
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag]; 
     
    if (cell==nil ) { 
        cell=[[[ UITableViewCell alloc ] initWithFrame : CGRectZero 
                                        reuseIdentifier:tag] autorelease]; 
    }     
     
    NSUInteger row=[indexPath row]; 
     
    //設置文本  
    cell.text =[listData objectAtIndex :row]; 
     
    //選中後的顏色又不發生改變,進行下面的設置  
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;   
     
    //不需要分割線  
    //tableView.separatorStyle=UITableViewCellSeparatorStyleNone;    
     
    return cell; 
     

 
#pragma mark – Table view data delegate  
 
//響應用戶單擊事件  
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
     
    UIAlertView* showSelection; 
    NSString* message; 
     
    message = [[NSString alloc]initWithFormat:@"You chose the : %@", 
                     [self.listData objectAtIndex:indexPath.row]]; 
     
    showSelection = [[UIAlertView alloc] 
                     initWithTitle:@"Selected" 
                     message:message 
                     delegate:nil 
                     cancelButtonTitle:@"OK" 
                     otherButtonTitles:nil];    
     
    [showSelection autorelease]; 
    [showSelection show]; 

作者:ztp800201
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *