1. 程式人生 > >UITableView的一些基本用法

UITableView的一些基本用法

UITableView繼承自UIScrollView

第一步: 在你的Controller的後面加上<UITableViewDelegate, UITableViewDataSource>

第二步: 將UITableView的delegate設定為self

第三步: 實現這些delegate的方法,

1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;   

這個方法將返回有多少個section

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
      return 1;     //number of the sections
}

2. - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

這個方法將返回每個section有多少元素, 一般我們宣告一個數組, 把將要顯示的內容放置到數組裡面, 這裡的section的數量一般要和array.count一致.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
{  
    return array.count;   //number of rows in section
}  

3.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;

這個方法將返回指定row的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

返回指定section的header view 的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(

NSInteger)section;

返回指定section的footer view 的高度

4. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

返回指定row 的cell。這個地方是比較關鍵的地方,一般在這個地方來定製各種個性化的 cell元素。

這裡只是使用最簡單最基本的cell 型別。

cell.textLabel :主標題

cell.detailTextLabel: 副標題

cell.imageView: 每個cell最前面還可以插入image  

cell.accessoryType: 設定右邊的圖示,通過可以設定是飽滿的向右的藍色箭頭,還是單薄的向右箭頭,還是對勾標記.

每一個UITableView裡都維護著一個cell佇列,當UITableView剛載入的時候,cell佇列裡是沒有任何資料的。dequeueResableCellWithIdentifier從字面上理解就是”出列可重用的cell",也就是根據一個標識identifier從cell佇列裡取出一個UITableViewCell,當然了,如果cell佇列裡沒有此標識的cell,呼叫此方法的結果就是返回nil。因此,在UITableView剛載入的時候,cell佇列裡沒有可用的cell,所以必須通過語句下面的if迴圈中的語句來建立一個新的cell.

當UITableView在滾動的時候導致UITableViewCell滾出手機螢幕檢視的時候,程式會將這一個UITalbeViewCell例項放入此UITableView所維護的cell佇列中。當UITableview中有新的UITableViewCell需要展現在手機螢幕檢視上時,就會呼叫tableView:cellForRowAtIndexPath:方法了,因此我們可以知道以下幾點:
1-重取出來的cell是有可能已經捆綁過資料或者加過子檢視的,所以,如果有必要,要清除資料(比如textlabel的text)和remove掉add過的子檢視(使用tag)
2-這樣設計的目的是為了避免頻繁的 alloc和delloc cell物件而已,沒有多複雜
3-設計的關鍵是實現cell和資料的完全分離

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{  
    static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";  
    UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
    if (cell == nil)  
    {  
        // Create a cell to display an ingredient.
	//建立對應CellIdentifier標識的UITableViewCell例項  
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   
                                       reuseIdentifier:showUserInfoCellIdentifier]   
                autorelease];  
    }  
      
    // Configure the cell.  
    [email protected]"簽名";  
    cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];  
        }  




5. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

返回指定的section的header的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
{  
    if (section ==0)  
        return 80.0f;  
    else  
        return 30.0f;  
}  


6.- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

返回指定的section 的 header  的 title,如果這個section header  有返回view,那麼title就不起作用了。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
{  
    if (tableView == tableView_)  
    {  
        if (section == 0)   
        {  
            return @"title 1";  
        }   
        else if (section == 1)   
        {  
            return @"title 2";  
        }   
        else   
        {  
            return nil;  
        }  
    }   
    else   
    {  
        return nil;  
    }  
}  


7. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

返回指定的 section header 的view,如果沒有,這個函式可以不返回view

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
{  
    if (section == 0)   
    {  
          
        UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"   
                                                        owner: self  
                                                      options: nil] lastObject];  
       
        else  
        {  
           return nil;  
        }  
}  


8.  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

當用戶選中某個行的cell的時候,回撥用這個。但是首先,必須設定tableview的一個屬性為可以select 才行

TableView.allowsSelection=YES;  
cell.selectionStyle=UITableViewCellSelectionStyleBlue; 
如果不希望相應select, 那麼就可以用下面的程式碼設定屬性
TableView.allowsSelection=NO;  
 下面是響應select 點選函式,根據哪個section,哪個row 自己做出響應就好了
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
{  
    if (indexPath.section == 1)   
    {  
        return;  
    }  
    else if(indexPath.section==0)  
    {  
        switch (indexPath.row)   
        {  
            //聊天  
            case 0:  
            {  
                [self  onTalkToFriendBtn];  
            }  
                break;  
                  
            default:  
                break;  
        }  
    }  
    else   
    {  
        return ;  
    }  
      
}  

如何讓cell 能夠響應 select,但是選中後的顏色又不發生改變呢,那麼就設定 

cell.selectionStyle = UITableViewCellSelectionStyleNone;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    //cell被選中後的顏色不變  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;  
}  

9. 如何設定tableview  每行之間的分割線
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
如果不需要分割線,那麼就設定屬性為 UITableViewCellSeparatorStyleNone  即可。

10.如何設定 tableview cell的背景顏色

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
        //設定背景顏色  
        cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];  
}  

11.- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

這個函式響應,使用者點選cell 右邊的 箭頭(如果有的話)

12.如何設定tableview 可以被編輯

               首先要進入編輯模式:

[TableView setEditing:YES animated:YES]; 
如果要退出編輯模式,肯定就是設定為NO
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
返回當前cell  要執行的是哪種編輯,下面的程式碼是 返回 刪除  模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return UITableViewCellEditingStyleDelete;  
}  

  -(void) tableView:(UITableView *)aTableViewcommitEditingStyle:(UITableViewCellEditingStyle) editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath;

通知告訴使用者編輯了 哪個cell,對應上面的程式碼,我們在這個函式裡面執行刪除cell的操作。

-(void) tableView:(UITableView *)aTableView  
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle  
forRowAtIndexPath:(NSIndexPath *)indexPath  
{  
        [chatArray  removeObjectAtIndex:indexPath.row];  
    [chatTableView  reloadData];  
}  

13. 如何獲得 某一行的CELL物件

 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;



相關推薦

UITableView一些基本用法

UITableView繼承自UIScrollView 第一步: 在你的Controller的後面加上<UITableViewDelegate, UITableViewDataSource> 第二步: 將UITableView的delegate設定為self 第三

關於HTML框架(frameset)的一些基本用法

www. 否則 pan 不同 set標簽 不一定 vue.js 相同 border frameset 定義 W3C是這樣定義frameset框架的,通過使用框架,你可以在同一個瀏覽器窗口中顯示不止一個頁面。每份HTML文檔稱為一個框架,並且每個框架都獨立於其他的框架。註意,

android AndoridManifest.xml 一些基本用法

AndoridManifest.xml 是一個xml 具體是什麼東西 百度比我解釋的好 我這邊只是一些常用的用法 首先上下程式碼 在這裡插入程式碼片 <application android:allowBackup="true"

集合框架--Map集合的一些基本用法

package cn.itcast.api.a.map; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Ma

Tensorflow的一些基本用法

在使用TensorFlow中會遇到一些其基本的用法,再次作為記錄備忘! tf.add_to_collection 在計算整體的loss是會將不同部分的loss放入一個集合中,最後計算整體的loss,因此會用到tf.add_to_collection,具體

C++的一些基本用法

這篇部落格可能不是像通常的程式設計書籍一樣,從“hello world”進行一個視覺化的一個例程,主要就是對一些C++的特性,包括關鍵字的一些理解,也是用到一個整理一個,所以邏輯性和連貫性可能不是很好,純粹作為C++的一個知識積累,如果當中有不正確的地方,還請批

Hibernate中的Query一些基本用法

/*** 新增*/public void save(Stu stu){   try {    tran=this.GetSession().beginTransaction();    this.GetSession().save(stu);    tran.commit(

vim的一些基本用法

標題 首先vim有三種模式 1命令模式 2插入模式 3退出模式 一 vim的命令模式配置下 p 貼上 u 撤銷 ctrl+r 恢復撤銷 :set 模式資訊 :set nu 新增行號 :set nonu 取消行號 set mouse=a 設定滑鼠可操作 set cursorlin

shutil的一些基本用法

import shutil import time import tarfile # 將檔案內容拷貝到另一個檔案中 shutil.copyfileobj(open('a1', 'r'), open('random2', 'w')) # 複製檔案 shutil.copyfile('a1', 'a2')

Jquery的一些基本用法

1、獲取指定name的radio選定的值:var a=$("input[name='price']:checked").attr("value");2、獲取指定id的html標籤中的文字內容:var a=$("#aa").text();3、修改指定id的html標籤中的內容,HTML程式碼也會以文字形式顯示:

jQuery一些基本用法

1、關於頁面元素的引用通過jquery的$()引用元素包括通過id、class、元素名以及元素的層級關係及dom或者xpath條件等方法,且返回的物件為jquery物件(集合物件),不能直接呼叫dom定義的方法。2、jQuery物件與dom物件的轉換只有jquery物件才能使

MySQL的一些基本用法

用法檢視資料庫 show databases; 建立資料庫 create database 庫名; 刪除資料庫 drop database 庫名; 選擇要使用的資料庫 use 庫名; 檢視庫裡的表 show tables; 查看錶結構 de

path的一些基本用法

Paint的基本實用方法和技巧(1)基本的使用1.1 負責圖形繪製相關//重置mPaint.reset();mPaint.setColor(Color.RED);mPaint.setAlpha(255);//設定畫筆的樣式mPaint.setStyle(Paint.Styl

介紹WPF中DependencyProperty的一些基本用法

在程式中使用DependencyProperty好的,我相信你已經決定了要使用DependencyProperty而不是傳統的CLR屬性,正如上一篇Post所說,很多地方都需要使用到DependencyProperty,作為例子,我決定定義一個MyBorde

JS 一些基本用法

以下是引用: http://blog.csdn.net/themaster/archive/2006/07/30/1001096.aspx  1. oncontextmenu="window.event.returnValue=false" 將徹底遮蔽滑鼠右鍵<tabl

shell腳本一些基本語句的用法

shell 語句一.for語句1.使用for語句批量添加用戶2.使用for語句檢查主機是否可以正常ping通二:while語句1.使用while語句按用戶名有規律添加用戶2.使用while語句編寫猜價格腳本三:case語句1.使用case語句編寫測試字符類型腳本2.使用case語句編寫服務狀態控制腳本本文出自

C/C++中一些基本的輸入輸出用法

1.scanf()與printf() %c格式能夠識別空格和換行並將其輸入 %s通過空格和換行來識別字符串的結束 2.getchar()與putchar() 用來輸入和輸出單個字元 3.gets()與puts() 用來輸入一行字串,gets()識別換行符來

Spring_MVC 一些基本註釋作用和用法

1.1 接受頁面資料 @RequestMapping("/add") public String add(Bookinfo bookinfo) { //呼叫業務層新增bookSerice.save(bookinfo); System.out.println(bo

UITableView介紹 之 基本用法

UITableView簡介   UITableView是我們ios開發中最常用的一個控制元件,學會UItableView的使用對我們的開發工作也是相當重要。 基本用法   使用Xcode新建一個空專案,在預設的UIViewController中顯示我們

ubuntu一些基本操作用法!(轉)

如何列出硬碟分割表? 請參閱 #基本備註 sudo fdisk -l 您可以使用 系統 -> 管理 -> Disks [編輯] 如何列出硬碟檔案系統使用狀況? 請參閱 #基本備註 df -T -h 您可