iOS開發之UITableView的使用

來源:互聯網
上載者:User

標籤:reload   prot   normal   產生   queue   處理   程式   elf   建立   

這一篇記錄的是iOS開發中UITableView的使用,iOS中的UITableView跟Android中的ListView特別相似,以下用一個Demo來說明:

1、Xcode中建立projectTestSimpleTableViewproject

2、在Main.storyboard中拖入一個UITableView控制項

3、在ViewController.h檔案裡,實現UITableViewDelegate和UITableViewDataSource協議

這裡須要說下的是。為了給UITableView填充資料,須要讓ViewController實現這兩個協議,類似於Android中給ListView填充資料。須要為ListView指定一個Adapter,然後重寫Adapter中的某些方法,iOS中也是一樣的,實現了上面這兩個協議後,須要實現這兩個協議中的某些方法,才幹為UITableView加入資料。

ViewController.h檔案的代碼例如以下:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>//介面中的UITableView控制項@property (weak, nonatomic) IBOutlet UITableView *tableView;//UITableView中的資料,用一個字串數組來儲存@property (strong, nonatomic) NSMutableArray *tableDataArr;@end

為了將ViewController.h中聲明的tableView變數,跟Main.storyboard中的UITableView控制項聯絡起來,須要在Main.storyboard中做一些處理,例如以所看到的:


用滑鼠右鍵點擊Main.storyboard介面中的ViewController小圓鈕,然後將Outlets中的tableView拖到面板中的UITableView控制項上。這樣就將變數和控制項建立起聯絡了。

和Android中不同的是,要將變數和布局檔案裡的控制項建立聯絡。我們是使用findViewById方法,通過控制項的ID找到相應的控制項。

4、以下須要在ViewController.m檔案裡,處理一些資料,首先須要在viewDidLoad方法裡載入幾條測試的資料。代碼例如以下:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //在這裡載入一些資料。用於顯示在UITableView上面    [self loadData];}#pragma mark 載入一些資料,用於顯示在UITableView上- (void)loadData {    //初始化數組    self.tableDataArr = [NSMutableArray array];    //加入20個字串到數組中    for(int i = 0; i < 20; i++) {        [self.tableDataArr addObject:[NSString stringWithFormat:@"table item %i", i]];    }}
這裡是直接用迴圈產生了20條TableView資料。

5、實現UITableViewDataSource中的兩個方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   和

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

代碼例如以下:

<span style="font-size:14px;">#pragma mark 該方法返回UITableView中的item個數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [self.tableDataArr count];}#pragma mark 該方法返回UITableView中每一個儲存格,在這裡處理每一個儲存格中該顯示什麼資料- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    //從隊列中取出儲存格    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];    //為儲存格的label設定資料    cell.textLabel.text = [self.tableDataArr objectAtIndex:indexPath.row];    return cell;}</span>

這裡的identifier是我們定義在方法外部的一個靜態變數:

static NSString *identifier =@"TableViewCell";

這個變數的作用,就類似於Android中給一個ListItem設定tag,主要是為了重用TableViewCell而為單元格指定一個標識,通過這個標識就能找到單元格。

6、到這裡代碼基本上就寫完了,可是UITableView怎麼知道該從哪裡擷取資料呢。在Android中為ListView指定資料,須要寫一個適配器Adapter,然後調用ListView的setAdapter方法指定資料,在iOS中為UITableView指定資料,能夠在Main.storyboard中,滑鼠右鍵放到UITableView上面。然後拖拉到ViewController小圓鈕上面,例如以所看到的:



然後鬆開滑鼠右鍵,在出現的對話方塊中選擇dataSource。再反覆一次上面的過程,選擇delegate。這樣就給UITableView設定了資料來源和託付,託付主要用來處理UITableView的點擊等事件,實現了託付中的某些方法就可以處理這些事件。

除了使用上面的操作方式為UITableView指定資料來源之外。還能夠直接在代碼中設定UITableView的資料來源,能夠在viewDidLoad方法中增加以下兩行代碼:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //在這裡載入一些資料,用於顯示在UITableView上面    [self loadData];    //不在Main.storyboard中設定資料來源和託付的話,就用以下兩行代碼設定TableView的資料來源和託付    self.tableView.delegate = self;    self.tableView.dataSource = self;}
7、假設這時候直接執行應用的話,會報錯例如以下:

Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘unable to dequeue a cell with identifier TableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard‘

原因是我們沒有為UITableView指定單元格,還缺少以下一步:

在Main.storyboard中拖一個TableViewCell控制項到UITableView上面,例如以所看到的:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >

然後選中這個TableViewCell,在右側配置這個TableViewCell的標識,例如以所看到的:

注意這裡填寫的identifier要跟我們定義在代碼中的那個identifier一致。這樣才幹正確載入UITableView,再次執行應用程式後。結果例如以下所看到的:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >

這裡應該是有切割線的,可能在模擬器上顯示不出來,所以在中沒有顯示出切割線。

8、給每一個單元格加上一個圖片,這裡能夠通過以下的方法來給project加入一個圖片:


然後在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中增加設定圖片的代碼例如以下所看到的:

#pragma mark 該方法返回UITableView中每一個儲存格。在這裡處理每一個儲存格中該顯示什麼資料- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    //從隊列中取出儲存格    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];    //為儲存格的label設定資料    cell.textLabel.text = [self.tableDataArr objectAtIndex:indexPath.row];    //為儲存格設定一個圖片    cell.imageView.image = [UIImage imageNamed:@"icon.png"];    return cell;}
這樣就能夠給單元格加片了,效果例如以所看到的:



9、假設想給單元格加上一個側滑刪除的功能,須要實現協議中的一個方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

然後在這種方法中增加刪除的代碼例如以下:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    [self.tableDataArr removeObjectAtIndex:indexPath.row];    [self.tableView reloadData];}
增加上面的代碼後就可以實現側滑刪除的功能了。


iOS開發之UITableView的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.