欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

講解iOS開發(fā)中UITableView列表設(shè)計(jì)的基本要點(diǎn)

 更新時(shí)間:2016年01月21日 09:54:08   作者:芳草小腳丫  
這篇文章主要介紹了講解iOS開發(fā)中UITableView列表設(shè)計(jì)的基本要點(diǎn),其中對(duì)列表行操作的常用操作舉例是iOS開發(fā)中經(jīng)常用到的基礎(chǔ),需要的朋友可以參考下

一、UITableView簡(jiǎn)單介紹

   1.tableView是一個(gè)用戶可以滾動(dòng)的多行單列列表,在表視圖中,每一行都是一個(gè)UITableViewCell對(duì)象,表視圖有兩種風(fēng)格可選

復(fù)制代碼 代碼如下:

typedef NS_ENUM(NSInteger, UITableViewStyle) {
    UITableViewStylePlain,                  // regular table view
    UITableViewStyleGrouped                 // preferences style table view
};

201612193806481.png (628×1000)

 2.表視圖還可為其添加索引值,比如通訊錄中右側(cè)索引列表,每一個(gè)索引項(xiàng)對(duì)應(yīng)其節(jié)頭標(biāo)題

201612193837352.png (320×568)201612193854714.png (320×568)

這兩種形式的列表下面還會(huì)介紹到。
3.最簡(jiǎn)單的一種表視圖是一個(gè)選擇列表,可以限制選擇一列或多列,如上圖右邊。  

 4.頁眉和頁腳,可以根據(jù)自己的需要,對(duì)tableView設(shè)置頁眉和頁腳的內(nèi)容

201612193918210.png (320×568)

二、UITableViewCell

    1. UITableViewCell是表視圖的單元格,系統(tǒng)會(huì)緩存可見的行。通過完成UITableViewDataSource協(xié)議中必須完成的代理方法CellForRowAtIndexPath方法來填充表視圖上單元格數(shù)據(jù)。

    2. UITableViewCell有四種樣式可選   

復(fù)制代碼 代碼如下:

UITableViewCellStyleDefault,    // 簡(jiǎn)單包含一個(gè)可選的imageView和一個(gè)label顯示文本
UITableViewCellStyleValue1, // 包含可選的imageView,一個(gè)textLabel和一個(gè)detailLabel,其中detailLabel位置在最左,右對(duì)齊,文本顏色為藍(lán)色
UITableViewCellStyleValue2,     //包含一個(gè)textLabel和一個(gè)detailLabel,textLabel默認(rèn)為藍(lán)色文本,右對(duì)齊,detailLabel的位置緊挨著textLabel右邊,默認(rèn)文本左對(duì)齊,顏色為黑色  
UITableViewCellStyleSubtitle    // 包含可選的imageView,一個(gè)textLabel,一個(gè)detailLabel,其中detailLabel在textLabel下方,字體較小,默認(rèn)顏色為黑色,左對(duì)齊

三、創(chuàng)建簡(jiǎn)單TableView

1. 先給出效果圖

201612194009636.png (320×568)201612194022150.png (320×568)

2. 創(chuàng)建方式及代碼(本文只講述代碼創(chuàng)建)

        a) 創(chuàng)建一個(gè)Single  View Application,命名為"tableView"

        b) 新建一個(gè)繼承自UITableView的類,關(guān)于tableView的實(shí)現(xiàn)將全部寫在這個(gè)類中(當(dāng)然也可直接在對(duì)      應(yīng)所需要用得ViewController中創(chuàng)建,分離出來的好處是可以在將tableView的方法單獨(dú)放在一個(gè)類中,當(dāng)ViewController的代碼量比較大或者這個(gè)table需要在多個(gè)地方使用時(shí)推薦使用),命名為general_table_view.

        c) 代碼

①在general_table_view.h文件中,添加幾個(gè)屬性   

復(fù)制代碼 代碼如下:

@interface general_table_view : UITableView

// tableView的坐標(biāo)
@property (nonatomic, assign) CGRect        tableViewFrame;
 
// 存放Cell上各行textLabel值
@property (nonatomic, copy)NSMutableArray * textLabel_MArray;
 
// 存放Cell上各行imageView上圖片
@property (nonatomic, copy)NSMutableArray * images_MArray;
 
// 存放Cell上各行detailLabel值
@property (nonatomic, copy)NSMutableArray * subtitle_MArray;
 
@end


②在general_table_view.m的interface中聲明代理
復(fù)制代碼 代碼如下:

@interface general_table_view ()<UITableViewDataSource,UITableViewDelegate>
 
@end

③在.m中的initWithFrame方法內(nèi)部設(shè)置table的代理
復(fù)制代碼 代碼如下:

// Initialization code
        self.delegate   = self;
        self.dataSource = self;

以及添加tableViewFrame的set方法
復(fù)制代碼 代碼如下:

-(void)setTableViewFrame:(CGRect)tableViewFrame
{
    self.frame = tableViewFrame;// 設(shè)置tableView的frame為所傳值
}

④接下來實(shí)現(xiàn)tableView的dataSource和delegate方法

必須實(shí)現(xiàn)的方法有兩個(gè)

復(fù)制代碼 代碼如下:

// tableView每個(gè)分區(qū)的行數(shù),可以為各個(gè)分區(qū)設(shè)置不同的行數(shù),根據(jù)section的值判斷即可
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_textLabel_MArray count];
}
 
// 實(shí)現(xiàn)每一行Cell的內(nèi)容,tableView重用機(jī)制
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 為其定義一個(gè)標(biāo)識(shí)符,在重用機(jī)制中,標(biāo)識(shí)符非常重要,這是系統(tǒng)用來匹配table各行cell的判斷標(biāo)準(zhǔn),在以后的學(xué)習(xí)中會(huì)體會(huì)到
    static NSString *cellIdentifier = @"cellIdentifier";
    
    // 從緩存隊(duì)列中取出復(fù)用的cell
    UITableViewCell *cell           = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    // 如果隊(duì)列中cell為空,即無復(fù)用的cell,則對(duì)其進(jìn)行初始化
    if (cell==nil) {
        
        // 初始化
        cell                    = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
        // 定義其輔助樣式
        cell.accessoryType      = UITableViewCellAccessoryNone;
    }
 
    // 設(shè)置cell上文本內(nèi)容
    cell.textLabel.text         = [_textLabel_MArray objectAtIndex:indexPath.row];
    
    return cell;
}

⑤還有其他輔助方法,根據(jù)需要添加
復(fù)制代碼 代碼如下:

// tableView分區(qū)數(shù)量,默認(rèn)為1,可為其設(shè)置為多個(gè)分區(qū)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
 
// tableView頁眉的值,同理,可為不同的分區(qū)設(shè)置不同的頁眉,也可不寫此方法
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"頁眉";
}
 
// 頁腳
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"頁腳";
}

⑥在所需要添加的ViewController中添加tableView,在ViewController.m方法中
復(fù)制代碼 代碼如下:

#import "general_table_view.h"
@interface ViewController ()
{
    general_table_view *table;// 聲明table
}
@end

并在ViewDidLoad方法中對(duì)其進(jìn)行初始化
復(fù)制代碼 代碼如下:

// 初始化
table                   = [[general_table_view alloc] initWithFrame:CGRectMake(0, 20, 320, self.view.frame.size.height-20) style:UITableViewStylePlain];
 
// 設(shè)置數(shù)據(jù)源
table.textLabel_MArray  = [[NSMutableArray alloc] initWithObjects:@"南京市",@"南通市",@"淮安市",@"鎮(zhèn)江市",@"揚(yáng)州市",@"常州市", nil];
 
 [self.view addSubview:table];// 添加到當(dāng)前View

⑦運(yùn)行即可得到圖5的效果,將初始化時(shí)的style改為UITableViewStyleGrouped即可得到圖6的效果
復(fù)制代碼 代碼如下:

// 初始化
    table                   = [[general_table_view alloc] initWithFrame:CGRectMake(0, 20, 320, self.view.frame.size.height-20) style:UITableViewStyleGrouped];

四、為每一行添加圖片

在ViewController.m的ViewDidLoad方法中設(shè)置數(shù)據(jù)源時(shí),在addSubview之前,初始化一個(gè)存放圖片的數(shù)組,這里我添加的是同一張圖片,如果想為每一行設(shè)置不同的圖片,添加不同的圖片到數(shù)組中即可

復(fù)制代碼 代碼如下:

NSMutableArray *images  = [NSMutableArray array];
for(NSInteger index = 0;index<[table.textLabel_MArray count];index++){
    UIImage *image      = [UIImage imageNamed:@"2"];
        
    [images addObject:image];
}
table.images_MArray     = [[NSMutableArray alloc] initWithArray:images];

在CellForRowAtIndexPath方法中設(shè)置textLabel值部分添加
復(fù)制代碼 代碼如下:

// 設(shè)置cell上文本內(nèi)容
    cell.textLabel.text         = [_textLabel_MArray objectAtIndex:indexPath.row];
// 設(shè)置每一行的圖片
    cell.imageView.image        = [_images_MArray objectAtIndex:indexPath.row];

五、列表的其他樣式

在CellForRowAtIndexPath方法中,初始化Cell時(shí)改變cell的style和accessoryType,style,style默認(rèn)有四種可選。

在ViewController的ViewDidLoad方法中添加圖片的for循環(huán)中為數(shù)組添加值

復(fù)制代碼 代碼如下:

NSMutableArray *subtitle= [NSMutableArray array];
    
    for(NSInteger index = 0;index<[table.textLabel_MArray count];index++){
        
        UIImage *image      = [UIImage imageNamed:@"2"];
        
        NSString *detail    = [NSString stringWithFormat:@"detail text %d",index+1];
        
        [images addObject:image];
        
        [subtitle addObject:detail];
    }
table.subtitle_MArray   = [[NSMutableArray alloc] initWithArray:subtitle];

并在CellForRowAtIndexPath方法初始化時(shí)將
復(fù)制代碼 代碼如下:

UITableViewCellStyleDefault改變成其他三種樣式,并添加代碼

// 設(shè)置小標(biāo)題
   cell.detailTextLabel.text   = [_subtitle_MArray objectAtIndex:indexPath.row];


效果圖如下:

201612194314852.png (320×568)201612194333280.png (320×568)

201612194409290.png (320×568)201612194421916.png (320×568)

六、列表中行的操作
1.選中行

    實(shí)現(xiàn)代理方法

復(fù)制代碼 代碼如下:

// 選中行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"您點(diǎn)擊了第%d分區(qū)第%d行",indexPath.section, indexPath.row);
    
    // 取消選中狀態(tài)
    // [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

2.刪除行

    要對(duì)行進(jìn)行操作,首先要實(shí)現(xiàn)代理方法

復(fù)制代碼 代碼如下:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

    先講述單獨(dú)刪除一行數(shù)據(jù),即左滑出現(xiàn)刪除按鈕,并刪除行的操作,后文會(huì)介紹多選批量刪除

201612194438332.png (320×568)

可重置刪除按鈕的標(biāo)題,默認(rèn)為"delete"

復(fù)制代碼 代碼如下:

// 設(shè)置刪除按鈕標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}
點(diǎn)擊刪除后

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 從數(shù)據(jù)源中刪除
    [self.dataArray removeObjectAtIndex:indexPath.row];
    
    // 從列表中刪除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
   
}


3.插入行

 ①這時(shí)我將插入行和刪除行都以一個(gè)按鈕動(dòng)作來觸發(fā),點(diǎn)擊后tableView進(jìn)入編輯模式,先上效果圖

201612194459745.png (320×568)201612194912499.png (320×568)

 ②在ViewDidLoad中添加代碼,其中self.addButton和self.deleteBarButtonItem均在storyBoard中創(chuàng)建,下文中的按鈕也是這種情況

復(fù)制代碼 代碼如下:

NSArray *leftBarButtons = [NSArray arrayWithObjects:self.addButton,self.deleteBarButtonItem, nil];
    
self.navigationItem.leftBarButtonItems = leftBarButtons;//設(shè)置導(dǎo)航欄左邊按鈕為添加和刪除按鈕

 ③在@interface中聲明一個(gè)變量
復(fù)制代碼 代碼如下:

UITableViewCellEditingStyle selectEditingStyle;

④兩個(gè)按鈕的點(diǎn)擊事件
復(fù)制代碼 代碼如下:

// 更新導(dǎo)航欄按鈕
-(void) updateBarButtons
{
    if (self.tableView.editing==YES) {
        self.navigationItem.rightBarButtonItem = self.doneBarButtonItem;
    }
}
// 點(diǎn)擊添加按鈕
- (IBAction)addButtonClicked:(id)sender {
    selectEditingStyle = UITableViewCellEditingStyleInsert;
    
    [self.tableView setEditing:YES animated:YES];
    
    [self updateBarButtons];
    
}
// 點(diǎn)擊刪除按鈕
- (IBAction)deleteButtonClicked:(id)sender {
    
    
    selectEditingStyle = UITableViewCellEditingStyleDelete;
    
    [self.tableView setEditing:YES animated:YES];
    
    [self updateBarButtons];
}

 ⑤實(shí)現(xiàn)相應(yīng)的代理方法
復(fù)制代碼 代碼如下:

// 是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
// 編輯模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return selectEditingStyle;
    
}
 
 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 刪除模式
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        
        // 從數(shù)據(jù)源中刪除
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 刪除行
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    // 添加模式
    else if(editingStyle == UITableViewCellEditingStyleInsert){
        
        // 從數(shù)據(jù)源中添加
        [self.dataArray insertObject:@"new iPhone" atIndex:indexPath.row];
        
        // 添加行
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic ];
    }
   
}
// 點(diǎn)擊完成按鈕
- (IBAction)doneButtonClicked:(id)sender {
    
    [self.tableView setEditing:NO animated:YES];
    
    [self updateBarButtons];
}

4.移動(dòng)行

 ①效果圖

201612194936788.png (320×568)

 ②在tableView進(jìn)入編輯模式時(shí),可以對(duì)行進(jìn)行移動(dòng)操作,通過方法

復(fù)制代碼 代碼如下:

// 是否支持移動(dòng)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
③設(shè)置行可移動(dòng),并完成移動(dòng)行方法,改變數(shù)據(jù)源

// 移動(dòng)行操作-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{// 這里其實(shí)就是數(shù)組中兩個(gè)變量交換位置的過程 id object = [self.dataArray objectAtIndex:fromIndexPath.row];
    
    [self.dataArray removeObjectAtIndex:fromIndexPath.row];
    
    [self.dataArray insertObject:object atIndex:toIndexPath.row];
}


5、批量刪除行

①即完成可以選擇多個(gè)行之后批量刪除,如圖

201612195014090.png (320×568)201612195026332.png (320×568)

②在ViewDidLoad中添加代碼

self.navigationItem.rightBarButtonItem = self.editBarButtonItem;// 在右導(dǎo)航欄中添加編輯按鈕
③現(xiàn)在需要達(dá)到,點(diǎn)擊編輯按鈕在右上角出現(xiàn)取消按鈕,左上角出現(xiàn)刪除按鈕。并在選擇時(shí),能出現(xiàn)刪除行的數(shù)量,修改updateBarButtons方法,并添加一個(gè)方法來根據(jù)條件修改刪除按鈕的標(biāo)題

復(fù)制代碼 代碼如下:

// 更新導(dǎo)航欄按鈕
-(void) updateBarButtons
{
    // 如果是允許多選的狀態(tài),即進(jìn)入批量刪除模式
    if (self.tableView.allowsSelectionDuringEditing == YES) {
        //更新刪除按鈕
        [self updateDeleteButtonTitle];
        // 導(dǎo)航欄左邊按鈕設(shè)置為空
        self.navigationItem.leftBarButtonItems = nil;
        // 將左邊按鈕設(shè)置為'批量刪除'按鈕
        self.navigationItem.leftBarButtonItem = self.multiDeleteBarButton;
        // 導(dǎo)航欄右鍵設(shè)置為'取消'鍵
        self.navigationItem.rightBarButtonItem = self.cancelBarButtonItem;
        
        return;
    }
    if (self.tableView.editing==YES) {// 如果是編輯狀態(tài),且不屬于批量刪除狀態(tài)
        // 導(dǎo)航欄右鍵設(shè)置為'取消'鍵
        self.navigationItem.rightBarButtonItem = self.doneBarButtonItem;
    }
    else {// 如果不是編輯狀態(tài),將導(dǎo)航欄設(shè)置為初始狀態(tài)的樣式,即左欄為'添加','刪除'按鈕,右欄為'編輯'按鈕
        NSArray *leftBarButtons = [NSArray arrayWithObjects:self.addButton,self.deleteBarButtonItem, nil];
        self.navigationItem.leftBarButtonItems = leftBarButtons;
        
        self.navigationItem.rightBarButtonItem = self.editBarButtonItem;
    }
}

// 更新刪除按鈕的標(biāo)題
-(void)updateDeleteButtonTitle
{
    NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];//得到選中行
    
    BOOL allItemsAreSelected = selectedRows.count == self.dataArray.count;// 是否全選
    
    BOOL noItemsAreSelected = selectedRows.count == 0;// 選中行數(shù)是否為零
    
    if (allItemsAreSelected || noItemsAreSelected)
    {// 如果是全選或者未選,則刪除鍵為刪除全部
        self.multiDeleteBarButton.title = @"刪除全部";
    }
    else
    {// 否則 刪除鍵為刪除(選中行數(shù)量)
        self.multiDeleteBarButton.title = [NSString stringWithFormat:@"刪除 (%d)", selectedRows.count];
    }
 
}


④在
復(fù)制代碼 代碼如下:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
兩個(gè)方法中調(diào)用updateDeleteButtonTitle方法

⑤點(diǎn)擊編輯按鈕時(shí)
復(fù)制代碼 代碼如下:

// 編輯按鈕
- (IBAction)editButtonClicked:(id)sender { 
    self.tableView.allowsMultipleSelectionDuringEditing = YES;// 進(jìn)入可多選刪除狀態(tài)
    
    [self.tableView setEditing:YES animated:YES];// 將table設(shè)置為可編輯
    
    [self updateBarButtons];  //更改導(dǎo)航欄的導(dǎo)航按鈕
}

⑥點(diǎn)擊刪除多個(gè)按鈕時(shí)
復(fù)制代碼 代碼如下:

- (IBAction)multiDeleteClicked:(id)sender {
    // 選中的行
    NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
    
    // 是否刪除特定的行
    BOOL deleteSpecificRows = selectedRows.count > 0;
    // 刪除特定的行
    if (deleteSpecificRows)
    {
        // 將所選的行的索引值放在一個(gè)集合中進(jìn)行批量刪除
        NSMutableIndexSet *indicesOfItemsToDelete = [NSMutableIndexSet new];
        
        for (NSIndexPath *selectionIndex in selectedRows)
        {
            [indicesOfItemsToDelete addIndex:selectionIndex.row];
        }
        // 從數(shù)據(jù)源中刪除所選行對(duì)應(yīng)的值
        [self.dataArray removeObjectsAtIndexes:indicesOfItemsToDelete];
        
        //刪除所選的行
        [self.tableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else
    {
        // 刪除全部
        [self.dataArray removeAllObjects];
        
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    // 刪除完成,退出編輯狀態(tài),并退出多選狀態(tài),同時(shí)更新導(dǎo)航欄的按鈕
    [self.tableView setEditing:NO animated:YES];
    
    self.tableView.allowsMultipleSelectionDuringEditing = NO;
    
    [self updateBarButtons];
}

相關(guān)文章

最新評(píng)論