美文网首页iOS学习
UITableView使用指南

UITableView使用指南

作者: 一粒吗 | 来源:发表于2015-10-26 23:33 被阅读173次

    本文转自:http://steven.427.blog.163.com/blog/static/10236738020123264221032/

    一、概述

    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同生同死

    styleproperty // 有两种 UITableViewStylePlain, UITableViewStyleGrouped,经常用

    – numberOfRowsInSection://一个section有多少行,经常用

    – numberOfSections//一个TableView有多少个section,经常用

    rowHeightproperty // 行高,和tableView:heightForRowAtIndexPath:有性能上的区别

    separatorStyleproperty // cell之间的分割线?待确认

    separatorColorproperty // 同上

    backgroundViewproperty // tableview的背景view, 这个背景view在所有cell, header views, footer views之后

    tableHeaderViewproperty // tableview上方的一个headerView, 和delete里的section header不是一个概念

    tableFooterViewproperty // tableview下方的一个footerview

    sectionHeaderHeightproperty // section Header的高度,

    sectionFooterHeightproperty // sectjion Footer的高度

    sectionIndexMinimumDisplayRowCountproperty // 功能待确认? 参考例子: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: //反选一行,有何用?

    allowsSelectionproperty //是否允许用户选取一行

    allowsMultipleSelectionproperty // 是否选取多行,缺省为NO. 可以试试YES后的效果,哈哈

    allowsSelectionDuringEditingproperty // 编辑模式时是否可选取一行

    allowsMultipleSelectionDuringEditingproperty // 编辑模式时可否选取多行

    6) 插入、删除、移动行和sections

    – beginUpdates // 和endUpdates一起用,让插入、删除、选择操作同时动画,没用过

    – endUpdates //

    – insertRowsAtIndexPaths:withRowAnimation: //根据indexPath数组插入行

    – deleteRowsAtIndexPaths:withRowAnimation: //根据indexPath数组删除行

    – moveRowAtIndexPath:toIndexPath: //移动一行到另一行

    – insertSections:withRowAnimation: //插入sections

    – deleteSections:withRowAnimation: //删除sections

    – moveSection:toSection: //移动section

    7) 管理和编辑cell

    editingproperty // 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) 管理委托和数据源 (重要)

    dataSourceproperty // 通常会这么用: myTableView.delegate = self; self 为viewController

    delegateproperty // 通常会这么用: myTableView.dataSource = self; self 为viewController

    相关文章

      网友评论

        本文标题:UITableView使用指南

        本文链接:https://www.haomeiwen.com/subject/rhexhttx.html