UITableView詳細註釋

来源:http://www.cnblogs.com/ldnh/archive/2016/03/19/5296501.html
-Advertisement-
Play Games


style

 //普通
  UITableViewStylePlain,  
 //分組
  UITableViewStyleGrouped

//表格視圖
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //設置數據源
    tableView.dataSource = self;
    //設置代理
    tableView.delegate = self;
    //分區頭的高度
    tableView.sectionHeaderHeight = 30;
    //分區尾的高度
    tableView.sectionFooterHeight = 30;

    //行高,預設行高是44。
//    tableView.rowHeight = 100;
//    tableView.backgroundColor = [UIColor grayColor];

/*
        
         UITableViewCellSeparatorStyleNone 沒有線
         UITableViewCellSeparatorStyleSingleLine  單行線
         UITableViewCellSeparatorStyleSingleLineEtched  被石化的單行線
         
         
        //線的風格
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//        _tableView.separatorColor = [UIColor redColor];
        //線的內邊距
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

//行高固定時使用
        _tableView.rowHeight = 60;

    
    //設置背景View
//    UIImageView * imageView = [[UIImageView alloc] initWithFrame:tableView.bounds];
//    imageView.image = [UIImage imageNamed:@"baby.jpg"];
//    tableView.backgroundView = imageView;

//索引區域的背景
        _tableView.sectionIndexBackgroundColor = [UIColor whiteColor];
        //設置索引文字的顏色
        _tableView.sectionIndexColor = [UIColor blueColor];
        //cell行數小於多少是展示索引
        _tableView.sectionIndexMinimumDisplayRowCount = 100;
        //選擇索引時的背景顏色
        _tableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor];

//設置編輯模式
    [self.tableView setEditing:YES animated:YES];
    
//得到所有選中的行數
    NSArray * deleteList = [self.tableView indexPathsForSelectedRows];


代理  UITableViewDataSource,UITableViewDelegate
//幾個section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.peopleList.count;
}

//Section是分組,Rows多少行,預設情況下只有一個Section.分組的index為0

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray * p = self.peopleList[section];
    
    return p.count;
}

//cell單元格,IndexPath索引

//UITableViewCell是組成UITableView的單元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    cell.textLabel.text = self.peopleList[indexPath.section][indexPath.row];
    
    //clearColor是透明顏色
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

//返回分區頂部標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return self.headList[section];
}

//返回分區尾部標題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    
    return self.footList[section];
}

//選中某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //取消選中某一行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

//轉到下一頁
    NSString * vcName = self.viewControllers[indexPath.section][indexPath.row];
    
    UIViewController * vc = [[NSClassFromString(vcName) alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

    
}

//section空隙之間的顏色
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * v = [[UIView alloc] init];
    v.backgroundColor = [UIColor greenColor];
    return v;
}


//當滾動表格時,這個代理方法,一直調用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //UITableViewCell的復用機制
    
    static NSString * identifier = @"cellID";
    
    //從復用池裡找對應的cell
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        
        /*
         
         UITableViewCellStyleDefault  顯示圖片,顯示輔助圖片,顯示一行文字
         UITableViewCellStyleValue1   顯示圖片,顯示輔助圖片,普通文字,描述文字,共占一排
         UITableViewCellStyleValue2   不顯示圖片,顯示輔助圖片,普通文字,描述文字,共占一排
         UITableViewCellStyleSubtitle 顯示圖片,顯示輔助圖片,普通文字,描述文字,共占兩排
         
         */
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"];
    }
    
    Industry * industry = self.dataList[indexPath.row];
    
    cell.textLabel.text = industry.name;
    
    cell.detailTextLabel.text = industry.state;
    
    cell.imageView.image = [UIImage imageNamed:industry.icon];
    
    cell.accessoryType = UITableViewCellAccessoryDetailButton;
    
    return cell;
}


//設置行高方法,如果實現這個代理方法,rowHeight無效,這個主要用於設置可變cell高度
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    return 100;
//}


//顯示索引的題目

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self.dataList valueForKey:@"title"];
}

//返回表格視圖是否可以編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

//返回表格視圖是否可以滾動

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

//確定編輯的選項
/*
    UITableViewCellEditingStyle
 
    UITableViewCellEditingStyleNone  不編輯
    UITableViewCellEditingStyleDelete 刪除
    UITableViewCellEditingStyleInsert 插入
 */

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //如果是刪除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //從數據源刪除數據
        [self.dataList removeObjectAtIndex:indexPath.row];
        
        //根據indexPath數組刪除元素
        
        NSArray * deleteIndexs = @[indexPath];
        
        //UITableViewRowAnimation 操作動畫
        [tableView deleteRowsAtIndexPaths:deleteIndexs withRowAnimation:UITableViewRowAnimationFade];
        
    }
}



-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self.dataList removeObjectAtIndex:indexPath.row];
        
        //刷新表格
//        [tableView reloadData];
        
        //刷新刪除操作
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
        //插入操作
        [self.dataList insertObject:@"baby" atIndex:indexPath.row];
        
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}


//移動必須實現的方法
//sourceIndexPath 起始位置
//destinationIndexPath 目標位置

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    NSString * name = self.dataList[sourceIndexPath.row];
    
    [self.dataList removeObjectAtIndex:sourceIndexPath.row];

    [self.dataList insertObject:name atIndex:destinationIndexPath.row];
    
}

//修改刪除按鈕

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"真的要刪除嗎";
}


//預設返回UITableViewCellEditingStyleDelete

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    return UITableViewCellEditingStyleInsert;
    return UITableViewCellEditingStyleDelete;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //多選
    return UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete;
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • js的封裝分為以下幾種模式: 工廠模式,代碼如下: <!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="Generator" content="EditPlus®"><meta name="Author" co
  • 簡單介紹了HTML5 Canvas的來歷、瀏覽器相容性,主要內容是如何實現轉盤抽獎,最後的DEMO非常簡陋,真實場景還會有很多需要考慮和改進的地方(比如:瀏覽器首碼),這裡只講一步步實現的思路。
  • 今天做了個微博發佈欄的效果,涉及到的知識點包括以下: 1.判斷IE的方法:直接用 var ie=!-[1,];即可 2.連續發生事件的用法: IE下:觸發對象.onpropertychange 標準下:觸發對象.oninput 3.焦點聚集和移開事件。onfocus和onblur 4.判斷單位元組(0
  • 在寫網頁過程中,會遇到這種需要多個圖標展示: 如上圖中的「女裝」文字左邊的圖標。容易想到的解決方法是為每張圖片加入<img>標簽,但這樣做會增加HTTP請求數量,影響網站載入速度。比這更優的解決方案是:雪碧圖sprite。 所謂雪碧圖就是把我們所需要的所有小圖片用CSS sprite或者PS工具拼接
  • 1、讓用戶輸入兩個數字,然後輸出相加的結果。 *prompt() 方法用於顯示可提示用戶進行輸入的對話框。 語法: 說明: 如果用戶單擊提示框的取消按鈕,則返回 null。如果用戶單擊確認按鈕,則返回輸入欄位當前顯示的文本。 在用戶點擊確定按鈕或取消按鈕把對話框關閉之前,它將阻止用戶對瀏覽器的所有輸
  • 三種情況:1、行內元素(文本、圖片等) 給父元素設置text-align:center;來實現 2、定寬塊狀元素 3、不定寬塊狀元素(3種方法) (1)為需要水平居中的元素加table標簽;然後為table設置樣式: table{margin:0 auto;} (2)將塊級元素轉換為內聯元素(eg:
  • 引言 使用JavaScript能夠完整迅速做出Web應用程式,目前一套工具包括MongoDB、ExpressJS,AngularJS和Node.js越來越受到歡迎,其開發的靈活性和易用性加快開發效率,簡化開發者的工作。 在今天你有很多架構可以選擇建立一個Web應用,你需要的是快速開發,提高效率和註重
  • 整理了JavaScript中函數Function的各種,感覺函數就是一大對象啊,各種知識點都能牽扯進來,不單單是 Function 這個本身原生的引用類型的各種用法,還包含執行環境,作用域,閉包,上下文,私有變數等知識點的深入理解。 函數中的return Function類型函數實際上是對象,每個函
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...