iOS TableView常用小知识

iOS开发了不少的项目,几乎每个项目都会用到tableview,同时经常直接使用系统的是不行的,我们经常对其进行修改他的各种属性,以来达到我们所想要的结果,但是虽然用过不少次,但是每次使用经常会忘记某些特殊功能该如何设置,每次都需要再次google,所以便有了这篇博客,本篇博客,只是针对iOS开发tableview的各个功能需求开发的一种详细介绍!

系统cell样式

  • UITableViewCellStyleDefault //textLabel(偏左) - imageView(偏右)
  • UITableViewCellStyleValue1 // imageView(左) - textLabel(中) - detailText(右)
  • UITableViewCellStyleValue2 // textLabel (左) - detailText (右) no imageView
  • UITableViewCellStyleSubtitle //imageView(左)-textLabel(中上)-DetailText(中下)

UITableview是否显示分割线

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//不显示
  • UITableViewCellSeparatorStyleNone //不显示分割线
  • UITableViewCellSeparatorStyleSingleLine //显示简单的分割线
  • UITableViewCellSeparatorStyleSingleLineEtched //显示简单分割线,但是仅在分组中使用

UITableview 右侧箭头

cell.accessoryType = UITableViewCellAccessoryNone; //不显示右侧箭头 
  • UITableViewCellAccessoryNone //不显示右侧箭头
  • UITableViewCellAccessoryDisclosureIndicator //常见的简单的灰色小箭头
  • UITableViewCellAccessoryDetailDisclosureButton //蓝色底色白色箭头小button
  • UITableViewCellAccessoryCheckmark //是个小对号

无数据不显示多余的cell行数(设置尾视图)

[tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

UITableview指定显示某行

NSIndexPath *indexPath_1=[NSIndexPath indexPathForRow:arrIndex inSection:0];
NSArray *indexArray=[NSArray arrayWithObject:indexPath_1];
[_tableview reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];

处理系统cell添加图片会自适应大小问题

//想要改变系统cell图片自适应大小,直接把图片进行重绘,弄成自己所需要的大小即可
CGSize size = CGSizeMake(50, 50);
UIGraphicsBeginImageContextWithOptions(size, NO,0.0);
CGRect imageRect=CGRectMake(0.0, 0.0, size.width, size.height);
[[UIImage imageNamed:@"image"] drawInRect:imageRect];
cell.imageView.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

指定刷新某行或者某块cell

1
2
3
4
5
6
7
  //一个section刷新    
NSIndexSet * indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
//一个cell刷新
NSIndexPath * selectIndexPath=[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
NSArray *indexArry = @[selectIndexPath];
[tableView reloadRowsAtIndexPaths:indexArry withRowAnimation:UITableViewRowAnimationNone];

分割线左右对齐全屏显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//分割线左右对齐全屏
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
{
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
{
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)])
{
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}

UITableView取消选中状态

1
2
3
4
5
6

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
//其他代码
}

关闭UITableView cell的被选中效果

1
2

cell.selectionStyle = UITableViewCellSelectionStyleNone;
文章目录
  1. 1. 系统cell样式
  2. 2. UITableview是否显示分割线
  3. 3. UITableview 右侧箭头
  4. 4. 无数据不显示多余的cell行数(设置尾视图)
  5. 5. UITableview指定显示某行
  6. 6. 处理系统cell添加图片会自适应大小问题
  7. 7. 指定刷新某行或者某块cell
  8. 8. 分割线左右对齐全屏显示
  9. 9. UITableView取消选中状态
  10. 10. 关闭UITableView cell的被选中效果
,