UITableView回调和table相关成员方法详解

1.UITableView的datasource实现:

//回调获取每个section中的cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

//回调获取每个uitableviewcell,只有当需要显示的cell在table的可视区域内才被回调
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

//回调获取table的section数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//回调获取table的每个section的header标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

//回调获取table的每个section的footer标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

//回调判断指定的cell是否能有编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

//回调判断指定的cell能否被移动当进入编辑模式的时候
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

//回调获取table右边的索引栏内容
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

//当点击右边索引栏时执行的回调,可以根据点击的title值返回应该跳到第几个section
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

//当所有相关编辑模式的回调,只实现该回调时默认能滑动cell出现删除按钮。当和其他相关的编辑模式回调混合使用分别有移动,插入,删除等功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

//当进入的是移动的编辑模式时,实现该回调进行移动cell和数据的更新
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath


2.UITableView的delegate实现:

//每个cell将要呈现时回调
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

//每个section的header将要呈现时回调
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)

//每个section的footer将要呈现时回调
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)

//每个cell呈现完毕后回调
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0)

//每个section的header呈现完毕后回调
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)

//每个section的footer呈现完毕后回调
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0)

//回调设置每行的高度如果要自适应调整cell的高度,则必须要实现该回调,返回调整后的cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

//回调设置每个section的header高度,如果要自适应调整header的高度,则必须要实现该回调,返回调整后的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

//回调设置每个section的footer高度,如果要自适应调整footer的高度,则必须要实现该回调,返回调整后的footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

//回调设置每个section的header自定义view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

//回调设置每个section的footer自定义view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

//回调设置每行最右边的辅助按钮的样式
/*
 typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
 UITableViewCellAccessoryNone,                   // don't show any accessory view
 UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
 UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
 UITableViewCellAccessoryCheckmark               // checkmark. doesn't track
 };
 */

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0)

//回调设置辅助按钮被点击后的事件
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

//回调设置某行是否当被点击后处于高亮状态
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)

//回调当某行处于高亮状态时的行为
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)

//回调当某行失去高亮状态时的行为
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)

//回调某行将要被选中的行为
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

//回调某行将要被取消选中的行为
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)

//回调某行已经被选中点击的行为
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

//回调某行已经取消选中的行为
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)

//回调设置某行进入了哪种编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

//回调设置某行进入删除模式的删除按钮名字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)

//回调设置进入编辑模式的行能否缩进
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath

//回调将要进入编辑模式的行为
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

//回调完成编辑模式的行为
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

//回调设置某行的缩进级别
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

//回调设置某行被长按是否出现菜单栏
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0)

//回调设置菜单栏是否显示哪些菜单栏选项
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)

//回调点击菜单栏选项触发的事件
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)


3.UITableView常用成员方法


//通过indexpath获取指定行的uitableviewcell
    [_table cellForRowAtIndexPath:indexPath];
    //删除某行并带上动画
    [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //删除某个section并带上动画
    [_table deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationMiddle];
    //取消被选中的某行
    [_table deselectRowAtIndexPath:indexPath animated:YES];
    //返回某个section的header自定义view
    [_table headerViewForSection:indexPath.row];
    //返回某个section的footer自定义view
    [_table footerViewForSection:indexPath.row];
    //通过指定的cell获取cell所在的行数
    [_table indexPathForCell:cell];
    //通过在table的位置坐标返回该坐标所在的cell的行数
    [_table indexPathForRowAtPoint:CGPointMake(0, 0)];
    //返回被选中的cell的行数
    [_table indexPathForSelectedRow];
    //返回table中一个范围区域的cell的行数
    [_table indexPathsForRowsInRect:CGRectMake(0, 0, 100, 100)];
    //返回table视觉区域内的cell的行数
    [_table indexPathsForVisibleRows];
    //以动画方式在指定的位置插入cell
    [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    //以动画方式在指定位置插入section
    [_table insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];
    //返回指定位置的cell的rect属性
    [_table rectForRowAtIndexPath:indexPath];
    //重新加载table,重新执行所有回调方法
    [_table reloadData];
    //指定重新加载table的某些行,并以动画方式
    [_table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
    //重新加载索引栏
    [_table reloadSectionIndexTitles];
    //指定重新加载table的section,并以动画方式
    [_table reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    //滑动table到顶部
    [_table scrollsToTop];
    //带动画方式滑动table到指定区间
    [_table scrollRectToVisible:CGRectMake(0, 0, 100, 100) animated:YES];
    //带动画方式滑动table到指定位置附近
    [_table scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES];
    //以动画方式从table的哪个指定位置滑动到指定的行
    [_table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    //返回table可视区域内所有的cell
    [_table visibleCells];

    //beginupdates和endupdates要成对出现,对于同时操作多个insert,delete,selection的动画操作需要把它们放在beginupdates和endupdates中间执行,这样可以把多个动画合成一个动画来处理,实现原子性,降低出错的机率
      [_table beginUpdates];

      [_table endUpdates];

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UITableView的section中添加数据,你需要先创建一个包含所需数据的数组。然后,在UITableViewDataSource协议中实现以下方法: 1. numberOfSections(in tableView: UITableView) -> Int:返回表格中的section数。 2. tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int:返回指定section中的行数。 3. tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:返回指定indexPath的UITableViewCell实例。 例如,假设你有一个包含多个section的UITableView,每个section都包含一个字符串数组。以下是一个示例代码: ``` class ViewController: UIViewController, UITableViewDataSource { var data: [[String]] = [["item 1", "item 2"], ["item 3", "item 4", "item 5"]] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } // MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return data.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = data[indexPath.section][indexPath.row] return cell } } ``` 在这个例子中,我们创建了一个包含两个section的UITableView。每个section都有一个字符串数组,我们将其存储在data数组中。在numberOfSections方法中,我们返回data数组的数量,即section的数量。在tableView(_:numberOfRowsInSection:)方法中,我们返回特定section中的行数。最后,在tableView(_:cellForRowAt:)方法中,我们获取特定indexPath的字符串并将其显示在UITableViewCell中。 注意,在上述示例代码中,我们将UITableViewCell标识符设置为“Cell”,你需要确保在Storyboard或xib文件中对应的UITableViewCell的标识符也设置为“Cell”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值