UITableView 基本使用方法總結


1.、首先,Controller需要實現兩個  delegate ,分別是  UITableViewDelegate 和  UITableViewDataSource
2、然后 UITableView對象的 delegate要設置為 self。
3、 然后就可以實現這些delegate的一些方法拉。

(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;   這個方法返回 tableview 有多少個section 

//返回有多少個Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;  這個方法返回   對應的section有多少個元素,也就是多少行。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return 10;
}

(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,  還有一個 image在最前頭 叫 cell.imageView 還可以設置右邊的圖標,通過cell.accessoryType 可以設置是飽滿的向右的藍色箭頭,還是單薄的向右箭頭,還是勾勾標記。  

- (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.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:showUserInfoCellIdentifier] autorelease];
    }
    
    // Configure the cell.
    cell.textLabel.text=@"簽名";
    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;

如果不希望響應select,那么就可以用下面的代碼設置屬性:

cell.selectionStyle=UITableViewCellSelectionStyleBlue;

下面是響應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 *)aTableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(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;

 


免責聲明!

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



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