zoukankan      html  css  js  c++  java
  • UITableView / UITableViewDataSource / UITableViewDelegate

    一张图解释TableView各属性

    0

    UITableView

    Initializing a UITableView Object

     初始化:

    - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style

    Configuring a Table View

     配置:

    1. style

    /** TableView样式,默认Plain */

    @property(nonatomic, readonly) UITableViewStyle style

    typedef enum {

       UITableViewStylePlain,

       UITableViewStyleGrouped 

    } UITableViewStyle;

    2. - numberOfRowsInSection:

    /** 返回对应Section的rows number */

    - (NSInteger)numberOfRowsInSection:(NSInteger)section

    3. - numberOfSections

    /**  返回对应的Sections number */

    - (NSInteger)numberOfSections

    4. rowHeight

    /**  cell的高度 */

    @property(nonatomic) CGFloat rowHeight

    **可用tableView:heightForRowAtIndexPath:代替rowHeight

    5. separatorStyle

    /** cell分割线的样式,默认SingleLine */

    @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle

    typedef enum : NSInteger {

       UITableViewCellSeparatorStyleNone,

       UITableViewCellSeparatorStyleSingleLine,

       UITableViewCellSeparatorStyleSingleLineEtched 

    } UITableViewCellSeparatorStyle;

    6. separatorColor

    /** cell分割线颜色,默认灰色 */

    @property(nonatomic, retain) UIColor *separatorColor

    7. backgroundView

    /** tableView下的backgroundView */

    @property(nonatomic, readwrite, retain) UIView *backgroundView

     Tips: A table view’s background view is automatically resized to match the size of the table view. You must set this property to nil to set the background color of the table view.

     提示: tableView的backgroundView会自动拉伸覆盖整个tableView, 且backgroundView位于backgroundColor所设置的view的上层, 所以如果同时存在backgroundView和backgroundColor两个有效值, 需要把backgroundView设置为nil.

    8. separatorInset

    /** 分割线的内嵌距离 */

    @property(nonatomic) UIEdgeInsets separatorInset

     Inset只有设置左右距离会产生效果, 上下并无反应

    Creating Table View Cells

     创建cell

    1. - registerNib:forCellReuseIdentifier:

    /** 通过xib创建cell */

    - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier

     指明cell可以从哪个xib文件中加载出来

    2. - registerClass:forCellReuseIdentifier:

    /** 通过类创建cell */

    - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

     指明cell可以从哪个类中创建

    3. - dequeueReusableCellWithIdentifier:forIndexPath:

    /** 创建可重用cell */

    - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath

    registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier:Tips: If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier:  method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse  method instead.

     提示: 用该方法创建reuse的cell之前, 需要先调用-registerNib 或者 -registerClass, 若cell是从类中创建, 则会调用 initWithStyle:reuseIdentifier: 方法, 若Cell之前已经存在, 则会调用 prepareForReuse 方法.

    4. - dequeueReusableCellWithIdentifier:

    /** 创建可重用cell */

    - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

     使用该方法创建cell, identifier并没有找到相应的cell, 则不会创建成功, cell返回nil

    Accessing Header and Footer Views

    1. - registerNib:forHeaderFooterViewReuseIdentifier:

        - registerClass:forHeaderFooterViewReuseIdentifier:

    /** 创建headerView/footerView */

    - (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier

    - (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier

    - (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier

     与创建cell类似

    2. tableHeaderView/tableFooterView

    /** 获得当前tableHeaderView,默认为nil */

    /** 获得当前tableFooterView,默认为nil */

    @property(nonatomic, retain) UIView *tableHeaderView

    @property(nonatomic, retain) UIView *tableFooterView

    3. sectionHeaderHeight/sectionFooterHeight

    /** 获得section对应的Header/Footer高度 */

    @property(nonatomic) CGFloat sectionHeaderHeight

    @property(nonatomic) CGFloat sectionFooterHeight 

     不能与 tableView:heightForHeaderInSection:/tableView:heightForFooterInSection:同用,且只用在table的Style为Group的情况下才邮箱

    4. - headerViewForSection: / - footerViewForSection:

    /** 获得指定section下的header/footer */

    - (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section

    - (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section

    Accessing Cells and Sections

    1. - cellForRowAtIndexPath:

    /** 返回特定的cell, 用于设置各行的tableViewCell */

    - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

    2. - indexPathForCell:

    /** 获得cell对应的indexPath */

    - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell

    3. - indexPathForRowAtPoint:

    /** 根据特定点point获得该点下特定indexPath */

    - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point

    4. - indexPathForRowsInRect:

    /** 根据范围rect返回该范围下所有的indexPath */

    - (NSArray *)indexPathsForRowsInRect:(CGRect)rect

    5. - visibleCells

    /** 返回当前屏幕下显示的所有cell */

    - (NSArray *)visibleCells

    6. - indexPathsForVisibleRows

    /** 返回当前屏幕下显示的Rows所对应的所有indexPath */

    - (NSArray *)indexPathsForVisibleRows

    Estimating Element Heights

     估计高度

    1. estimatedRowHeight / estimatedSectionHeaderHeight / estimatedSectionFooterHeight

    /** 估计高度,用于延迟设置高度,默认为0 */

    @property(nonatomic) CGFloat estimatedRowHeight

    @property(nonatomic) CGFloat estimatedSectionHeaderHeight

    @property(nonatomic) CGFloat estimatedSectionFooterHeight

    Discussion

    Providing a nonnegative estimate of the height of rows can improve the performance of loading the table view. If the table contains variable height rows, it might be expensive to calculate all their heights when the table loads. Using estimation allows you to defer some of the cost of geometry calculation from load time to scrolling time.

    Scrolling the Table View

     滚动中的TableView

    1. - scrollToRowAtIndexPath:atScrollPosition:animated:

    /** 让tableView滚动到特定位置 */

    - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

     使用该方法不会调用scrollViewDidScroll:方法.

    typedef enum {

       UITableViewScrollPositionNone,

       UITableViewScrollPositionTop,

       UITableViewScrollPositionMiddle,

       UITableViewScrollPositionBottom 

    } UITableViewScrollPosition;

    2. - scrollToNearestSelectedRowAtScrollPosition:animated:

    /** 滚到最近row的特定位置,并没什么用.... */

    - (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

    Managing Selections

     点击事件处理

    1. - indexPathForSelectedRow / - indexPathsForSelectedRows

    /** 返回点击的indexPath */

    - (NSIndexPath *)indexPathForSelectedRow

    - (NSArray *)indexPathsForSelectedRows

    2. - selectRowAtIndexPath:animated:scrollPosition

    /** 手动选中指定位置的cell */

    - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath
                        animated:(BOOL)animated
                  scrollPosition:(UITableViewScrollPosition)scrollPosition

    Discussion

    Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor does it send UITableViewSelectionDidChangeNotification notifications to observers

     调用该方法不会引起代理方法和通知

    3. - deselectRowAtIndexPath:animated

    /** 取消选取 */

    - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath
                          animated:(BOOL)animated

    4. allowsSelection

    /** 是否允许被选择 */

    @property(nonatomic) BOOL allowsSelection

    5. allowsMultipleSelection

    /** 是否允许同时选中多个cell, 默认NO */

    @property(nonatomic) BOOL allowsMultipleSelection

    6. allowsSelectionDuringEditing

    /** 是否允许编辑状态下可以选中cell, 默认NO */

    @property(nonatomic) BOOL allowsSelectionDuringEditing

    7. allowsMultipleSelectionDuringEditing

    /** 是否允许编辑状态下同时选中多个cell */

    @property(nonatomic) BOOL allowsMultipleSelectionDuringEditing

    Inserting, Deleting, and Moving Rows and Sections

     编辑状态

    1. beginUpdates / endUpdates

    /** 编辑完成后调用该方法, 可以自行调用该方法以完成动画效果 */

    - (void)beginUpdates

    - (void)endUpdates

    2. - insertRowsAtIndexPaths:withRowAnimation: / deleteRowsAtIndexPaths:withRowAnimation

        - insertSections:withRowAnimation: / - deleteSections:withRowAnimation

    /** 在特定indexPath中插入/删除 */

    - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

    - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

    /** 在特定section中插入/删除 */

    - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

    - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

    3. - moveRowAtIndexPath:toIndexPath:

        - moveSection:toSection

    /** 把cell从某个位置移动到另一个位置 */

    - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath

    - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection

    Managing the Editing of Table Cells

     编辑cells

    1. editing / - setEditing:animated:

    /** 是否进入编辑状态 */

    @property(nonatomic, getter=isEditing) BOOL editing

    - (void)setEditing:(BOOL)editing animated:(BOOL)animate

    Reloading the Table View

     重载/更新

    1. - reloadData

    /** 重新加载tableView全部数据, 不应该在插入删除后调用 */

    - (void)reloadData

    It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates.

    2. - reloadRowsAtIndexPaths:withRowAnimation:

    /** 重载特定indexPaths的数据 */

    - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

    typedef enum {

       UITableViewRowAnimationFade,

       UITableViewRowAnimationRight,

       UITableViewRowAnimationLeft,

       UITableViewRowAnimationTop,

       UITableViewRowAnimationBottom,

       UITableViewRowAnimationNone,

       UITableViewRowAnimationMiddle,

       UITableViewRowAnimationAutomatic = 100

    } UITableViewRowAnimation;

    3. - reloadSections:withRowAnimation

    /** 重载特定sections的数据 */

    - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

    4. - reloadSectionIndexTitles

    /** 重载section的序号 */

    - (void)reloadSectionIndexTitles

    Discussion

    This method gives you a way to update the section index after inserting or deleting sections without having to reload the whole table.

    Accessing Drawing Areas of the Table View

     组件位置

    - (CGRect)rectForSection:(NSInteger)section

    - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath

    - (CGRect)rectForFooterInSection:(NSInteger)section

    - (CGRect)rectForHeaderInSection:(NSInteger)section

    Managing the Delegate and the Data Source

     代理/数据源

    1. dataSource

    /** 设置数据源 */

    @property(nonatomic, assign) id< UITableViewDataSource > dataSource

    2. delegate

    /** 设置代理 */

    @property(nonatomic, assign) id< UITableViewDelegate > delegate

    Notifications

    UITableViewSelectionDidChangeNotification

    Posted when the selected row in the posting table view changes. There is no userInfo dictionary associated with this notification.

    TIPS:

    UITableView overrides the layoutSubviews method of UIView so that it calls reloadData only when you create a new instance of UITableView or when you assign a new data source.

    If you want to customize your table cell beyond the standard cell styles, you can set the cell style to custom and add your custom views to the cell’s contentView property programmatically.

    You can customize the appearance of your header or footer by setting a custom background view or tint color. The background view is placed behind the contentView and used to display static background content behind the header or footer.!!Avoid setting both a custom background view and a custom tint.

    UITableViewDataSource

    Configuring a Table View

     配置:

    1. - tableView:cellForRowAtIndexPath:

    /** 设置cell */

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    2. - numberOfSectionsInTableView:

    /** 设置section的数目, 默认为1 */

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    3. - tableView:numberOfRowsInSection:

    /** 设置特定Section中row的数目 */

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    4. - sectionIndexTitlesForTableView:

    /** 返回每个section对应的title */

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

    5. - tableView:sectionForSectionIndexTitle:atIndex:

    /** 根据section title和title的index 返回 section对应在tableView下的index */

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

    6. - tableView:titleForHeaderInSection: / - tableView:titleForFooterInSection:

    /** 返回特定section对应的header的title */

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

    Discussion

    The table view uses a fixed font style for section header titles. If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView:viewForHeaderInSection: instead.

     想更改header / footer 的字体, 调用delegate的方法 tableView:viewForHeaderInSection: / tableView:viewForFooterInSection:

    Inserting or Deleting Table Rows

     插入/删除

    1. - tableView:commitEditingStyle:forRowAtIndexPath:

    /** 提交编辑操作 */

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    typedef enum : NSInteger {

       UITableViewCellEditingStyleNone,

       UITableViewCellEditingStyleDelete,

       UITableViewCellEditingStyleInsert 

    } UITableViewCellEditingStyle;

      

     调用这个方法后, 根据插入或者删除操作, 会通知tableView方法 insertRowsAtIndexPaths:withRowAnimation: or deleteRowsAtIndexPaths:withRowAnimation:

     要实现横扫删除效果需要实现该方法, 可以用  performSelector:withObject:afterDelay:  实现延迟操作 

    2. - tableView:canEditRowAtIndexPath

    /** 设置对应indexPath位置下的Cell是否可以编辑 */

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

     可以通过代理方法 tableView:editingStyleForRowAtIndexPath: 返回 UITableViewCellEditingStyleNone 取消编辑状态

    Reordering Table Rows

     排序

    1. - tableView:canMoveRowAtIndexPath:

    /** 设置对应indexPath位置下的Cell是否可以移动 */

    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

    2. - tableView:moveRowAtIndexPath:toIndexPath:

    /** 移动cell */

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

    UITableViewDelegate

    Configuring Rows for the Table View

     配置:

    1. - tableView:heightForRowAtIndexPath:

    /** 设置cell的高度 */

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    2. - tableView:estimateHeightForRowAtIndexPath:

    /** 设置估计高度 */

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

    Return Value

     Return UITableViewAutomaticDimension if you have no estimate.

    3. - tableView:indentationLevelForRowAtIndexPath:

    /** 返回特定section的凹陷程度 */

    - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

    4. - tableView:willDisplayCell:forRowAtIndexPath:

    /** 创建cell前会调用该方法, 可以提前在这里设置一些值 */

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

    Managing Accessory Views

     指向标

    1. - tableView:editActionsForRowAtIndexPath:

    /** 自定义一个action */

    - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

    2. - tableView:accessoryButtonTappedForRowWithIndexPath:

    /** 用户点击accessory */

    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

    Managing Selections

    1. - tableView:willSelectRowAtIndexPath:

    /** 将要选中特定row */

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

    2. - tableView:didSelectRowAtIndexPath:

    /** 正在选中特定row */

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    3. - tableView:willDeselectRowAtIndexPath:

    /** 将要取消选中 */

    - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath

    4. - tableView:didDeselectRowAtIndexPath:

    /** 已经取消选中 */

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

    Modifying the Header and Footer of Sections

    1. - tableView:viewForHeaderInSection: / - tableView:viewForFooterInSection:

    /** 设置特定section上的header / footer */

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

    Discussion

    The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForHeaderInSection: / tableView:heightForFooterInSection: is also implemented.

    2. - tableView:heightForHeaderInSection: / - tableView:estimatedHeightForHeaderInSection:

    /** 设置特定section上header / footer的高度 */

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section

    3. - tableView:willDisplayHeaderView: / - tableView:willDisplayFooterView:

    /** 将要显示各个section的header时调用 */

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

    - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section

    Editing Table Rows

    1. - tableView:willBeginEditingRowAtIndexPath:

    /** 将要进入编辑模式前调用 */

    - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

    2. - tableView:didEndEditingRowAtIndexPath:

    /** 完成编辑后调用 */

    - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

    3. - tableView:editdingStyleForRowAtIndexPath:

    /** 设置特定indexPath下的编辑模式 */

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    typedef enum : NSInteger {

       UITableViewCellEditingStyleNone,

       UITableViewCellEditingStyleDelete,

       UITableViewCellEditingStyleInsert 

    } UITableViewCellEditingStyle;

    4. - tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:

    /** 修改特定indexPath下删除按钮的文字 */

    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

    Reordering Table Rows

    1. - tableView:targetIndexPathForMoveFromRowAtIndexPath:

    /** 拖动cell, 返回最后所在indexPath */

    - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

  • 相关阅读:
    ES 设置管理
    ES 数据搜索(1)
    ES 集群管理及基本操作
    ES 安装
    ES 基本概念
    HBase 缓存
    【Linux】【8】切换JDK版本时报错,bash: ./java: cannot execute binary file
    【Linux】【7】常用命令-目录处理命令
    【Linux】【6】Java项目打成Jar包后部署至服务器上
    【Linux】【5】安装jdk1.8并配置环境变量,以及切换jdk
  • 原文地址:https://www.cnblogs.com/easyToCode/p/4878693.html
Copyright © 2011-2022 走看看