1.UITableView的datasource实现:
1 //回调获取每个section中的cell的行数 2 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 3 4 //回调获取每个uitableviewcell,只有当需要显示的cell在table的可视区域内才被回调 5 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 6 7 //回调获取table的section数量 8 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 9 10 //回调获取table的每个section的header标题 11 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 12 13 //回调获取table的每个section的footer标题 14 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 15 16 //回调判断指定的cell是否能有编辑状态 17 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 18 19 //回调判断指定的cell能否被移动当进入编辑模式的时候 20 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 21 22 //回调获取table右边的索引栏内容 23 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 24 25 //当点击右边索引栏时执行的回调,可以根据点击的title值返回应该跳到第几个section 26 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 27 28 //当所有相关编辑模式的回调,只实现该回调时默认能滑动cell出现删除按钮。当和其他相关的编辑模式回调混合使用分别有移动,插入,删除等功能 29 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 30 31 //当进入的是移动的编辑模式时,实现该回调进行移动cell和数据的更新 32 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
2.UITableView的delegate实现:
1 //每个cell将要呈现时回调 2 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 3 4 //每个section的header将要呈现时回调 5 - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0) 6 7 //每个section的footer将要呈现时回调 8 - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0) 9 10 //每个cell呈现完毕后回调 11 - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0) 12 13 //每个section的header呈现完毕后回调 14 - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0) 15 16 //每个section的footer呈现完毕后回调 17 - (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0) 18 19 //回调设置每行的高度,如果要自适应调整cell的高度,则必须要实现该回调,返回调整后的cell高度 20 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 21 22 //回调设置每个section的header高度,如果要自适应调整header的高度,则必须要实现该回调,返回调整后的header高度 23 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 24 25 //回调设置每个section的footer高度,如果要自适应调整footer的高度,则必须要实现该回调,返回调整后的footer高度 26 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 27 28 //回调设置每个section的header自定义view 29 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 30 31 //回调设置每个section的footer自定义view 32 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 33 34 //回调设置每行最右边的辅助按钮的样式 35 /* 36 typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) { 37 UITableViewCellAccessoryNone, // don't show any accessory view 38 UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track 39 UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks 40 UITableViewCellAccessoryCheckmark // checkmark. doesn't track 41 }; 42 */ 43 - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0) 44 45 //回调设置辅助按钮被点击后的事件 46 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 47 48 //回调设置某行是否当被点击后处于高亮状态 49 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0) 50 51 //回调当某行处于高亮状态时的行为 52 - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0) 53 54 //回调当某行失去高亮状态时的行为 55 - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0) 56 57 //回调某行将要被选中的行为 58 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 59 60 //回调某行将要被取消选中的行为 61 - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) 62 63 //回调某行已经被选中点击的行为 64 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 65 66 //回调某行已经取消选中的行为 67 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) 68 69 //回调设置某行进入了哪种编辑模式 70 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 71 72 //回调设置某行进入删除模式的删除按钮名字 73 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) 74 75 //回调设置进入编辑模式的行能否缩进 76 - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 77 78 //回调将要进入编辑模式的行为 79 - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath 80 81 //回调完成编辑模式的行为 82 - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath 83 84 //回调设置某行的缩进级别 85 - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath 86 87 //回调设置某行被长按是否出现菜单栏 88 - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0) 89 90 //回调设置菜单栏是否显示哪些菜单栏选项 91 - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0) 92 93 //回调点击菜单栏选项触发的事件 94 - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0)
3.UITableView常用成员方法
1 //通过indexpath获取指定行的uitableviewcell 2 [_table cellForRowAtIndexPath:indexPath]; 3 //删除某行并带上动画 4 [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 5 //删除某个section并带上动画 6 [_table deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationMiddle]; 7 //取消被选中的某行 8 [_table deselectRowAtIndexPath:indexPath animated:YES]; 9 //返回某个section的header自定义view 10 [_table headerViewForSection:indexPath.row]; 11 //返回某个section的footer自定义view 12 [_table footerViewForSection:indexPath.row]; 13 //通过指定的cell获取cell所在的行数 14 [_table indexPathForCell:cell]; 15 //通过在table的位置坐标返回该坐标所在的cell的行数 16 [_table indexPathForRowAtPoint:CGPointMake(0, 0)]; 17 //返回被选中的cell的行数 18 [_table indexPathForSelectedRow]; 19 //返回table中一个范围区域的cell的行数 20 [_table indexPathsForRowsInRect:CGRectMake(0, 0, 100, 100)]; 21 //返回table视觉区域内的cell的行数 22 [_table indexPathsForVisibleRows]; 23 //以动画方式在指定的位置插入cell 24 [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 25 //以动画方式在指定位置插入section 26 [_table insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight]; 27 //返回指定位置的cell的rect属性 28 [_table rectForRowAtIndexPath:indexPath]; 29 //重新加载table,重新执行所有回调方法 30 [_table reloadData]; 31 //指定重新加载table的某些行,并以动画方式 32 [_table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; 33 //重新加载索引栏 34 [_table reloadSectionIndexTitles]; 35 //指定重新加载table的section,并以动画方式 36 [_table reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 37 //滑动table到顶部 38 [_table scrollsToTop]; 39 //带动画方式滑动table到指定区间 40 [_table scrollRectToVisible:CGRectMake(0, 0, 100, 100) animated:YES]; 41 //带动画方式滑动table到指定位置附近 42 [_table scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES]; 43 //以动画方式从table的哪个指定位置滑动到指定的行 44 [_table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 45 //返回table可视区域内所有的cell 46 [_table visibleCells]; 47 48 //beginupdates和endupdates要成对出现,对于同时操作多个insert,delete,selection的动画操作需要把它们放在beginupdates和endupdates中间执行,这样可以把多个动画合成一个动画来处理,实现原子性,降低出错的机率 49 [_table beginUpdates]; 50 51 [_table endUpdates];