iOS開發之UITableView的使用


這一篇記錄的是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上面,例如以下圖所看到的:



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

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

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

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];
}
增加上面的代碼后就可以實現側滑刪除的功能了。



免責聲明!

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



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