UITableView使用指南

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   

 

本文轉載至 http://blog.csdn.net/yu0089/article/details/8227402

 

一、概述 
UITableView是iOS開發比不可少也是最重要的一個控制項類。可以說任何一個做iOS開發的人都必須熟練使用和掌握它。本文主要就是提供一個學習使用TableView的指南。 
要說UITableView必須要介紹他的幾個親戚:UITableViewDelegate,UITableViewDataSource,UITableViewCell。其中前兩個是TableView遵守的兩個protocol(別告訴我你不知道啥叫protocol哦)。然後本文會再列出TableView最常用最重要的一些知識點。最後再介紹幾個參考例子。 


二、UITableView和它的親戚們 

1. UITableView 
參考: 
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html


1) 初始化 UITableView對象 
– initWithFrame:style:  // 代碼產生方式,如果你在nib裡加的tableview不需要使用這個方法 
2)配置TableView 
– dequeueReusableCellWithIdentifier: // 必須要實現的方法,與TableView同生同死 
  style  property // 有兩種 UITableViewStylePlain, UITableViewStyleGrouped,經常用 
– numberOfRowsInSection:  //一個section有多少行,經常用 
– numberOfSections  //一個TableView有多少個section,經常用 
  rowHeight  property // 行高,和tableView:heightForRowAtIndexPath:有效能上的區別 
  separatorStyle  property // cell之間的分割線?待確認 
  separatorColor  property // 同上 
  backgroundView  property // tableview的背景view, 這個背景view在所有cell, header views, footer views之後 
  tableHeaderView  property // tableview上方的一個headerView, 和delete裡的section header不是一個概念 
  tableFooterView  property // tableview下方的一個footerview 
  sectionHeaderHeight  property // section Header的高度, 
  sectionFooterHeight  property // sectjion Footer的高度 
  sectionIndexMinimumDisplayRowCount  property //  功能待確認? 參考例子:  TheElements 
3) 訪問Cells和Sections 
– cellForRowAtIndexPath: //根據IndexPath返回cell 
– indexPathForCell: //根據cell返回它的indexPath,和上面的方法互補 
– indexPathForRowAtPoint://根據一個幾何點返回indexPath,如果超過邊界返回nil 
– indexPathsForRowsInRect: //根據一個幾何的矩形返回矩形所覆蓋的行,返回是一個indexPath數組 
– visibleCells // 不清楚怎麼用,待確認 
– indexPathsForVisibleRows //同上 
4) 滾動TableView 
– scrollToRowAtIndexPath:atScrollPosition:animated: // 滾動到指定位置 
– scrollToNearestSelectedRowAtScrollPosition:animated: // 同上 
5) 管理sections 
– indexPathForSelectedRow //返回選定行的indexPath,單行 
– indexPathsForSelectedRows //返回選定行的indexPath數組,多行 
– selectRowAtIndexPath:animated:scrollPosition: //根據indexPath選擇一行 
– deselectRowAtIndexPath:animated: //反選一行,有何用? 
  allowsSelection  property //是否允許使用者選取一行 
  allowsMultipleSelection  property // 是否選取多行,預設為NO. 可以試試YES後的效果,哈哈 
  allowsSelectionDuringEditing  property // 編輯模式時是否可選取一行 
  allowsMultipleSelectionDuringEditing  property // 編輯模式時可否選取多行 
6) 插入、刪除、移動行和sections 
– beginUpdates // 和endUpdates一起用,讓插入、刪除、選擇操作同時動畫,沒用過 
– endUpdates // 
– insertRowsAtIndexPaths:withRowAnimation: //根據indexPath數組插入行 
– deleteRowsAtIndexPaths:withRowAnimation: //根據indexPath數組刪除行 
– moveRowAtIndexPath:toIndexPath: //移動一行到另一行 
– insertSections:withRowAnimation: //插入sections 
– deleteSections:withRowAnimation: //刪除sections 
– moveSection:toSection: //移動section 
7) 管理和編輯cell 
  editing  property // YES進入編輯模式,tableview cell會出現插入、刪除、重排序的控制項 
– setEditing:animated: //設定進入退出編輯模式 
8) 重新載入TableView 
– reloadData // 重建整個表,包括cells、header、footer,indexs 
– reloadRowsAtIndexPaths:withRowAnimation: // 改進,不用reload整個表 
– reloadSections:withRowAnimation: // 同上 
– reloadSectionIndexTitles // 同上 
9) 訪問TableView的畫圖區 
– rectForSection: // 返回指定section的矩形 
– rectForRowAtIndexPath: //返回indexPath指定行的矩形 
– rectForFooterInSection: // 返回section的footer矩形 
– rectForHeaderInSection: // 返回section的header矩形 
10) Registering Nib Objects for Cell Reuse 
– registerNib:forCellReuseIdentifier: // 
11) 管理委託和資料來源 (重要) 
  dataSource  property // 通常會這麼用: myTableView.delegate = self; self 為viewController 
  delegate  property // 通常會這麼用:     myTableView.dataSource = self; self 為viewController 
原文地址:http://blog.csdn.net/y041039/article/details/7351982

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.