UItableView一些小方法

分类: UITableView和UIScrollView 2014-09-05 09:17 2894人阅读 评论(1) 收藏 举报

目录(?)[+]

1、UItableView设置偏移量

通过设置tableView的偏移量,让列表默认滚动到某个位置,内涵段子里面的效果

[objc] view plaincopy

  1. [myTableView setContentOffset:CGPointMake(0, 100) animated:YES];

2、刷新某行cell的方法

有时候只需要刷新某行的cell的数据,完全没必要调用[tableView reloadData]刷新整个列表的数据,调用以下方法即可。。

[objc] view plaincopy

  1. NSIndexPath *indexPath_1=[NSIndexPath indexPathForRow:1 inSection:0];
  2. NSArray *indexArray=[NSArray  arrayWithObject:indexPath_1];
  3. [myTableView  reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];

3、改变分组列表之间的间距

方法一

[objc] view plaincopy

  1. 改变每一组之间的间距
  2. self.tableView.sectionHeaderHeight = 5;
  3. self.tableView.sectionFooterHeight = 0;

方法二

[objc] view plaincopy

  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  2. {
  3. return 5;
  4. }
  5. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  6. {
  7. return 5;
  8. }
  9. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  10. {
  11. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  12. return view;
  13. }
  14. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  15. {
  16. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  17. return view;
  18. }

4、iOS7上tableView的分割线左边间距为0

ios7里面tableViewCell上面的分割线,左边少了20个像素,用下面的方法,可以让分割线完整显示出来

[objc] view plaincopy

  1. if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
  2. [_tableView setSeparatorInset:UIEdgeInsetsZero];
  3. }

5、设置tableview的cell颜色

[objc] view plaincopy

  1. //方法一:
  2. cell.contentView.backgroundColor = [UIColor redColor];
  3. //方法二:
  4. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  5. UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
  6. bgview.opaque = YES;
  7. bgview.backgroundColor = [UIColor orangeColor];
  8. [cell setBackgroundView:bgview];
  9. //方法三:
  10. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  11. {
  12. cell.backgroundColor = [UIColor redColor];
  13. }

6、调整cell之间的距离

[objc] view plaincopy

  1. #pragma mark 重写setFrame方法,自己调整cell的frame
  2. - (void)setFrame:(CGRect)frame
  3. {
  4. // tableView边框的宽度 #define kTableBorderWidth 5
  5. // 更改x、宽度
  6. frame.origin.x = kTableBorderWidth;
  7. frame.size.width -= kTableBorderWidth * 2;
  8. // 更改顶部间距、每个cell之间的间距
  9. frame.origin.y += kTableTopBorderWidth;
  10. frame.size.height -= kTableViewCellMargin;
  11. [super setFrame:frame];
  12. }

7、cell选中和不选中显示不通背景

    1. cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"choose_item_selected.jpg"]];
    2. cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"choose_item.jpg"]];

8、设置 tableHeader 据表格顶部间距

m_tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);

m_tableView.scrollIndicatorInsets = UIEdgeInsetsMake(44, 0, 0, 0);

时间: 2024-07-28 20:31:54

UItableView一些小方法的相关文章

IOS 应用 退出的一个小方法

AppDelegate * app=(AppDelegate *)[[UIApplication sharedApplication]delegate]; UIWindow *window = app.window; [UIView animateWithDuration:1.0f animations:^{ window.alpha = 0; window.frame = CGRectMake(window.bounds.size.width/2.0, window.bounds.size.w

提高自信的30个小方法

你自信吗?面对挫折.压力.困惑.障碍时,我们难免会丧失气场,怀疑自己的能力,但没有真正的自信生活就没有底气和光亮.下面是提高自信的30个小方法,说不定对你有帮助哦~ 1.学习是件好事,所以报个学习班充实一下自己并且享受这种主动学习知识的乐趣.你会发现你的头脑越充实,你就越自信!如果你没有时间上夜校,那么每天晚上给自己留半个小时去阅读吧.我会选择阅读那些我从来没有接触到的领域.比如我是个学习理科的,我就会让自己读些管理学,经济学,政治,宗教的书籍.虽然有的时候我并不能理解书中的内容,但就是抱着一种

UITableView属性和方法

1.初始化一个UITableView 1 - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style 1 struct CGRect { 2 CGPoint origin; 3 CGSize size; 4 }; 5 typedef struct CGRect CGRect; 1 typedef enum { 2 UITableViewStylePlain, //平铺样式 3 UITableViewStyleGrouped //

(转)java中查找List的快捷小方法

相信java开发的程序员,经常会遍历list里的数据,来查找想要的数据.之后选出来在做处理,我这有个小方法在大量数据的情况下能更快捷,当然这方法只对菜鸟有点用,对老鸟来说也许有更好的方法,请指点 遍历list的时候请把list里的每条数据安一个关键字放到map当中去如: for (Map<String, String> subMap : resourceList) {     resourceMap.put(subMap.get("OBJECTNAME"), subMap)

(iOS)开发中收集的小方法

1.颜色转变成图片 - (UIImage *)createImageWithColor:(UIColor *)color {     CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);     UIGraphicsBeginImageContext(rect.size);     CGContextRef context = UIGraphicsGetCurrentContext();     CGContextSetFillColorWithCo

jQuery提供的小方法

jQuery提供的小方法: 1.选择器 + 事件 + 函数 = 复杂的交互 2.循环处理与选择器匹配的各个元素:each() $("#").each(function(){ $(this).unblind('click'); }); 3.隐藏:hide():显示:show(): 4.向上滑动并消失:slideUp():     slideDown(): slideToggle();为动作指示“如果原来朝上,就向下滑动:如果原来朝下,就向上滑动”: 5.淡出:fadeOut():淡入fa

IOS开发之页面间传值的几种小方法

最正经的页面间传值方式莫过于代理传值,今天写的几种小方法不包括代理传值,因为是自学的原因,现在都不知道这几种方法的优缺点,往知道的朋友指正一下: 第一种:属性传值:(这种方法在故事版无效,适合纯代码编写时使用,为什么???) 前提:比如说有两个视图控制器:AViewController和BViewController,他们之间想从A传值到B, ——>在AViewController.m文件中 (1)导入BViewController #import "BViewController.h&q

android 小方法

小方法 1.获取屏幕分辨率: 1 public class BaseTools { 2 3 public static int getWindowWidth(Context context) { 4 // 获取屏幕分辨率 5 WindowManager wm = (WindowManager) (context 6 .getSystemService(Context.WINDOW_SERVICE)); 7 DisplayMetrics dm = new DisplayMetrics(); 8 w

web开发 小方法1-禁止横向滚动

最近学了学做了个公司的网站  总结了一些小方法 比如取消横向滚动条并禁止的横向滚动 这样就可以吧超出的切掉让网页更和谐 在body 标签 body{ text-align: center; overflow:scroll; overflow-x:hidden} 加入该代码就行  overflow属性详情可以去 w3cSchool 或者菜鸟教程