UITableView 入門簡單使用 [xcode 4.4.1]




本篇學習目標:

1. 使用Storyboard 編輯View

2. 用UITableView顯示一個簡單的數組

3. 增加UITableView 事件

4. 給UITableView綁定數據源

5. 點擊每行,彈出信息窗口

接下跟我如下操作:

1. 首先啟動Xcode,新建一個工程 創建項目,選擇如下:

 

 

下一步,輸入產品名,兩個紅框要注意:

 

 

完成創建項目之后,界面如下:

 

 

 

2.  選中MainStoryboard.storyboard 打開View編輯窗口,按下1,2,3步刪除操作,把默認的ViewController刪除

 


 

3. 打開控件庫拖一下Table View Controller 到MainStoryboard.storyboard 中,如下圖所示:


 

4. 打開Table View Cell 設置屬性,注意:Identifier 這個必須設置參數,這里我們設置成:MovieCell

 


 

5. 刪除默認的控制器(Controller) 文件,如圖所示:

 

 

6. 添加需要的Controller文件,如圖所示:

 

 

 

 

7. 打開 myDataViewController.h  文件如下所示:

//
//   myDataViewController.h
//   listMovie
//
//   Created by tony on 12-8-29.
//   Copyright (c) 2012 年 chinapcc.com. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface myDataViewController : UITableViewController

//  添加數據源
@property (strong,nonatomic) NSMutableArray *listOfMovies;

@end

 

注意:以前有些教程需要這行代碼:<UITableViewDataSource,UITableViewDelegate>,現在的不需要了

#import <UIKit/UIKit.h>

//  注意:這是錯誤的代碼
@interface myDataViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>

@end

 

8. 打開myDataViewController.m  文件

@implementation myDataViewController
//  在上面的代碼下,添加如此一行:

@synthesize listOfMovies;

 

9. 視圖初始化加載的代碼

- ( void)viewDidLoad
{
    
    listOfMovies = [[NSMutableArray alloc] init];
    
    [listOfMovies addObject: @" I Love Tony "];
    [listOfMovies addObject: @" 美麗心靈 "];
    [listOfMovies addObject: @" 雨人 "];
    [listOfMovies addObject: @" 波拉克 "];
    [listOfMovies addObject: @" 暗物質 "];
    [listOfMovies addObject: @" 天才瑞普利 "];
    [listOfMovies addObject: @" 貓鼠游戲 "];
    [listOfMovies addObject: @" 香水 "];
    [listOfMovies addObject: @" 一級恐懼 "];
    [listOfMovies addObject: @" 心靈捕手 "];
    [listOfMovies addObject: @" 莫扎特傳 "];
    [listOfMovies addObject: @" 證據 "];
    [listOfMovies addObject: @" 海上鋼琴師 "];
    [listOfMovies addObject: @" 電鋸驚魂 "];
    [listOfMovies addObject: @" 沉默的羔羊 "];
    [listOfMovies addObject: @" 非常嫌疑犯 "];
    [listOfMovies addObject: @" 尋找弗羅斯特 "];

    
    [super viewDidLoad];

     //  Uncomment the following line to preserve selection between presentations.
    
//  self.clearsSelectionOnViewWillAppear = NO;
 
    
//  Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    
//  self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

 

 10. 釋放內存,手持設備內存是不夠的,不要忘記以下這些代碼:

- ( void)viewDidUnload
{
     //  釋放內存
    self.listOfMovies = nil;
    [super viewDidUnload];
     //  Release any retained subviews of the main view.
    
//  e.g. self.myOutlet = nil;
}

 

 11. 修改數據綁定代碼,詳細如下:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     //  默認有些行,請刪除或注 釋 #warning Potentially incomplete method implementation.
    
//  這里是返回的節點數,如果是簡單的一組數據,此處返回1,如果有多個節點,就返回節點 數
     return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     //  默認有此行,請刪除或注 釋 #warning Incomplete method implementation.
    
//  這里是返回節點的行數
     return [self.listOfMovies count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     //  大家還記得,之前讓你們設置的Cell Identifier 的 值,一定要與前面設置的值一樣,不然數據會顯示不出來
     static NSString *CellIdentifier =  @" MovieCell ";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
     //  根據行獲取數組中值
    NSString *value = [listOfMovies objectAtIndex:indexPath.row];
    
     //  把取得值,設置到Table中去
    cell.textLabel.text = value;
   
     return cell;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
     //  返回頂部標題
     return  @" 電影列表 ";
}

-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
     //  返回底部文字
     return  @" 作者:汪肄敏 ";
}

 

12. 不要忘記View 與Controller 做一個聯系

 

 

13. 到此時,程序已可以運行了,看一看成果吧


 

14. 不滿意,是不是前面添加一個圖片,更好看點,那就來吧

先拖一張圖片進項目中,如圖所示:


 

我的圖片如下:


 

修改數據綁定的代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     //  大家還記得,之前讓你們設置的Cell Identifier 的 值,一定要與前面設置的值一樣,不然數據會顯示不出來
     static NSString *CellIdentifier =  @" MovieCell ";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
     //  根據行獲取數組中值
    NSString *value = [listOfMovies objectAtIndex:indexPath.row];
    
     //  把取得值,設置到Table中去
    cell.textLabel.text = value;
    
     //  添加列表前部圖片(這兩行是新增的)
    UIImage *image = [UIImage imageNamed: @" Movie.ico "];
    cell.imageView.image = image;
   
     return cell;
}

 
搞定,收工,效果如下:


 

背景聲音:那個脫脫呀,點擊事件。。。

 

呵呵,還有一個事件,接着來吧

15.修改事件代碼,默認是為用戶生成代碼,只是沒有內容,我們來修改一下:

- ( void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  大家都懂的,不需要我解釋吧
    NSString *slect = [listOfMovies objectAtIndex:indexPath.row];
    NSString *msg = [[NSString alloc] initWithFormat: @"  你選擇是: %@  ",slect];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @" 電影選擇 "
                                                    message:msg
                                                    delegate:self
                                          cancelButtonTitle: @" OK "
                                          otherButtonTitles:nil, nil];
    
    [alert show];

}

 最終效果來了:


 

16. 按國際慣例,要附上代碼:源代碼下 載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM