您的位置:首页 > 产品设计 > UI/UE

UITableView使用指南1(呕心原创)

2014-11-12 16:53 211 查看
http://blog.csdn.net/y041039/article/details/7351982

一、概述

UITableView是iOS开发比不可少也是最重要的一个控件类。可以说任何一个做iOS开发的人都必须熟练使用和掌握它。本文主要就是提供一个学习使用TableView的指南。

要说UITableView必须要介绍他的几个亲戚:UITableViewDelegateUITableViewDataSourceUITableViewCell。其中前两个是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

小结:

一、概述

UITableView是iOS开发比不可少也是最重要的一个控件类。可以说任何一个做iOS开发的人都必须熟练使用和掌握它。本文主要就是提供一个学习使用TableView的指南。

要说UITableView必须要介绍他的几个亲戚:UITableViewDelegateUITableViewDataSourceUITableViewCell。其中前两个是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

小结:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: