码迷,mamicode.com
首页 > 其他好文 > 详细

熟用TableView

时间:2015-04-10 23:53:56      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

  1. self.title = @"分组表视图";
  2. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStylePlain];
  3. //设置tableView数据源代理
  4.     tableView.dataSource = self;
  5. //设置tableView的代理
  6.     tableView.delegate = self;
  7. [self.view addSubview:tableView];
  8.  
  9. // 设置tableView的头视图和尾部视图:这能设置头部视图的高度,x y 方向的坐标是改变不了的,宽度也无法修改,与tableView的宽度一样
  10. UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
  11. tableView.tableHeaderView = headerView;
  12. UIView *fooderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
  13. tableView.tableFooterView = fooderView;

16.// 设置组的头视图和尾部视图的高度

  1. tableView.sectionHeaderHeight = 50;
  2. tableView.sectionFooterHeight = 40;

19.//设置每个cell的高度

  1. tableView.rowHeight = 50;

21.// 每组头视图的高度

22.-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section;

23.// 每组尾部的高度

24.-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section;

25.// 设置tableView每一行cell的高度

26.-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

29.//设置分割线的颜色

  1. tableView.separatorColor = [UIColor redColor];

31.// 设置分割线的样式,默认有UITableViewCellSeparatorStyleSingleLine

  1. tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

33.// 设置背景视图和背景颜色

  1. tableView.backgroundColor = [UIColor grayColor];
  2. tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@“bj.jpg"]];

37.#pragma mark - UITableView 的协议方法.

38.// 设置tableView的组数,如果不写默认创建一组

39.- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

40.// 设置每一组单元格的个数

41.-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;

42.// 创建单元格UITableViewCell,也就是tablView显示的每一行的视图

43.-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

45.UITableViewCell*cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

47.// 设置每组头视图的标题

48.-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section;

49.// 设置每组尾部视图的标题

50.-(NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section;

51.// 自定义组的头视图

52.-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section;

53.//自定义组的尾部视图

54.-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section;

55.// 设置选择时的样式

56.cell.selectionStyle = UITableViewCellSelectionStyleGray;

57.// 设置选中后的背景视图

58. cell.selectedBackgroundView = view;

59.// 设置辅助图标样式

60. cell.accessoryType = UITableViewCellAccessoryDetailButton;

61.// 将自定义的视图添加到contentaView 上,不要直接添加到cell上

  1. [cell.contentView addSubview:titleLabel];

64.// 创建一个位置

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:2]。

// 获取指定位置的cell    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];

66.//当前在屏幕上显示的所有cell     visibleCells

67.NSArray *cellArray =  [_tableView visibleCells];

68.// 获得当前显示在屏幕的cell的所有indexPath    indexPathsForVisibleRows

69.NSArray *indexPathArray =  [_tableView indexPathsForVisibleRows];

71.// 滚动到指定位置的cell

72.//[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

73.// 刷新tableView, 也就是重新调用所有实现的协议方法

  1. [_tableView reloadData];

75.// 刷新指定indexPath的cell

76.[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

77.// 刷新指定section(组)

  1. NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:0];
  2. [_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];

81.// 添加索引视图

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

83.{

  1. return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H"];

85.}

86.// 设置索引视图文字的颜色

  1. tableView.sectionIndexColor = [UIColor whiteColor];

88.// 索引视图的背景颜色

  1. tableView.sectionIndexBackgroundColor = [UIColor grayColor];

90.// 点击索引视图后调用的方法,包含点击索引位置的标题和位置

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

92.// cell将要在屏幕上显示的时候调用

93.-(void)collectionView:(UICollectionView*)collectionView willDisplayCell:(UICollectionViewCell*)cell forItemAtIndexPath:(NSIndexPath *)indexPath;

94.// cell结束在屏幕上显示的时候调用

95.-(void)collectionView:(UICollectionView*)collectionView didEndDisplayingCell:(UICollectionViewCell*)cell forItemAtIndexPath:(NSIndexPath *)indexPath;

96.// 自定义cell 的编辑模式

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

98.{

  1. return UITableViewCellEditingStyleDelete; 
  2. }
  3. // 设置tableViewCell 是否可以移动
  4. -(BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
  5. // 移动cell时,调用此方法
  6. -(void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  7.     // 交换数组两个元素的位置
  8.     [_mutableArray exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
  9.     
  10. }
  11. // 设置tableView 的cell为可编辑状态
  12. -(BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
  13. // 设置tableView 的cell为可编辑状态
  14. -(BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
  15. 删除指定的cell
  16. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  17.  
  18. // 单元格复用 dequeueReusableCellWithIdentifier
  19. static NSString *indentify = @"newCell";
  20.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
  21. if (cell == nil) {
  22.         cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentify];
  23. // 1、注册普通类的cell
  24. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newCell"];
  25. NewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newCell" forIndexPath:indexPath];
  26. // 2、注册xib 的cell
  27. UINib *nib = [UINib nibWithNibName:@"NewCell" bundle:nil];
  28. [self.tableView registerNib:nib forCellReuseIdentifier:@“newCell"];
  29. NewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newCell" forIndexPath:indexPath];
  30. 故事版不许要注册。就这三种情况
  31.  
  32. // 加载xib文件创建的cell应该使用这种方式
  33.         cell = [[[NSBundle mainBundle] loadNibNamed:@"FontsCell" owner:self options:nil] lastObject];
  34. -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
  35.  

熟用TableView

标签:

原文地址:http://www.cnblogs.com/SilverWinter/p/SilverWinter_TableView.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!