IOS开发之UITableView的奇技

作者:Biaoac

age:保密

sex:直男

性格:低调沉稳,乖张内涵

博客背景:之前一直在使用UITableView,但是一直都只是初识,后来在不断的使用中找到了很多之前没有在意的东西,遂整理出来,当然,有很多还是看别人的博客中提到的点,我把他重踩一遍;

1.点击的时候显示选中状态,但状态一直村在,必须在点击下一个的时候取消选中状态

点击cell的时候调用

- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath;
//离开点击时调用,- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

一般用法是在didSelectRowAtIndexPath方法中加入
[tableView deselectRowAtIndexPath:indexPath animated:YES];
即点击cell时cell有背景色,如过没有选中另一个,则这个cell背景色一直在,
加入这句话效果是在点击结束后cell背景色消失。

- (void)tableView:(UITableView )tableView didDeselectRowAtIndexPath:(NSIndexPath )indexPath2.


2.cell选中时的背景颜色(默认灰色,现在好像只有无色和灰色两种类型了)

@property (nonatomic) UITableViewCellSelectionStyle selectionStyle;

UITableViewCellSelectionStyleNone,

UITableViewCellSelectionStyleBlue,

UITableViewCellSelectionStyleGray,

UITableViewCellSelectionStyleDefault

重要属性:

indexpath.row:行indexpath.section:组

separatorColor:分割线的颜色separatorStyle:分割线样式

用来自定义头部和尾部自定义view只需要设置高度,宽度设置无效

tableHeaderView;

tableFooterView;

table中展示的数据是从模型(或者其他来源-数据源)中取得的,因此要更改表格数据,只需要更改数据源中得数据,并刷新表格即可。(不要直接更改tableviewcell中textLabel中得text数据,这样上下拖动tableview还是会显示原来的数据)

UITableViewCell的常用属性

accessoryType:cell右边的指示器

accessoryView:可自定义右边的view

backgroundView:自定义背景view

backgroundColor//优先级低于backgroundView

selectedBackgroundView

3.显示右侧索引

- (NSArray )tableView{  
    NSMutableArray *indexs = [[NSMutableArray alloc] init];  
    for (int i = 0; i < kHeaderTitle.count; i++) {      
        [indexs addObject:kHeaderTitle[i]];  
      }  
      return indexs;
}

4.删除操作

一般这种Cell如果向左滑动右侧就会出现删除按钮直接删除就可以了。其实实现这个功能只要实现代理方法,只要实现了此方法向左滑动就会显示删除按钮。只要点击删除按钮这个方法就会调用。
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle) 
  editingStyle forRowAtIndexPath:(NSIndexPath )indexPath;
- (void)tableView:(UITableView )indexPath{    
    if (editingStyle == UITableViewCellEditingStyleDelete) {        
            [_titleArray removeObject:_titleArray[indexPath.row]];        
            [tableView deleteRowsAtIndexPaths:@[indexPath] 
                    withRowAnimation:UITableViewRowAnimationBottom];    
         }
 }

5.导航栏图片拉伸放大

01
还是创建控制器,控制器里面创建tableView,初始化其必要的代理方法使能其正常显示.
02
初始化tableView的时候让tableView向下偏移(偏移下来的那段放图片):
_tableView.contentInset = UIEdgeInsetsMake(backGroupHeight - 64, 0, 0, 0);
03
初始化图片,注意图片的frame设置,加载在tableView上
imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0,   
                 -backGroupHeight, kDeviceWidth, backGroupHeight)];
imageBg.image = [UIImage imageNamed:@"bg_header.png"];
[_tableView addSubview:imageBg];
04
根据滑动时的偏移量改变图片的frame,改变navigationBar的透明度
- (void)scrollViewDidScroll:(UIScrollView )scrollView{
  CGFloat yOffset = scrollView.contentOffset.y;
  CGFloat xOffset = (yOffset + backGroupHeight)/2;
  if (yOffset < -backGroupHeight) {
      CGRect rect = imageBg.frame;
      rect.origin.y = yOffset;
      rect.size.height = -yOffset;
      rect.origin.x = xOffset;
      rect.size.width = kDeviceWidth + fabs(xOffset)2;
      imageBg.frame = rect;
  }
  CGFloat alpha = (yOffset + backGroupHeight)/backGroupHeight;
  [self.navigationController.navigationBar setBackgroundImage:
              [self imageWithColor:[[UIColor orangeColor] colorWithAlphaComponent:alpha]] 
              forBarMetrics:UIBarMetricsDefault];
  titleLb.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:alpha];
}
05
渲染navigationBar颜色方法
- (UIImage )imageWithColor:(UIColor )color{
  //描述矩形
  CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  //开启位图上下文
  UIGraphicsBeginImageContext(rect.size);
  //获取位图上下文
  CGContextRef content = UIGraphicsGetCurrentContext();
  //使用color演示填充上下文
  CGContextSetFillColorWithColor(content, [color CGColor]);
  //渲染上下文
  CGContextFillRect(content, rect);
  //从上下文中获取图片
  UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext();
  //结束上下文
  UIGraphicsEndImageContext();
  return currentImage;
}

6.刷新界面

No。1.刷新整个TableVIew
[self.tableView reloadData];
?
注意:此处reloadData是刷新整个UITableView,有时候,我们可能需要局部刷新。比如:只刷新一个cell、只刷新一个section等等。这个时候在调用reloadData方法,虽然用户看不出来,但是有些浪费资源。
?
NO.2刷新局部cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableViewreloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil]
withRowAnimation:UITableViewRowAnimationFade];

注意:这是刷新第一个section的第一个cell很方便的一种方法,虽然看上去,代码变多了,但是很节省资源,尽量减少刷新频率,这也是在iOS开发中对UITableView的一种优化。
NO.3
局部刷新Section
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
上面这段代码是刷新第0个section。
NO.4
刷新动画
刷新UITableView还有几个动画:
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade, //淡入淡出
UITableViewRowAnimationRight, //从右滑入 // slide in from right (or out to right)
UITableViewRowAnimationLeft, //从左滑入
UITableViewRowAnimationTop, //从上滑入
UITableViewRowAnimationBottom, //从下滑入
UITableViewRowAnimationNone, // available in iOS 3.0
UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = 100// available in iOS 5.0. chooses an appropriate animation style for you
};
时间: 2024-07-30 11:50:55

IOS开发之UITableView的奇技的相关文章

iOS开发之UITableView全面解析

在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不开它强大的功能,今天这篇文章将针对UITableView重点展开讨论.今天的主要内容包括: 1.基本介绍 2.数据源 3.代理 4.性能优化 5.UITableViewCell 6.常用操作 7.UITableViewController 8.MVC模式   基本介绍 UITableView有两种风

iOS开发之UITableView的使用

这一篇记录的是iOS开发中UITableView的使用,iOS中的UITableView跟Android中的ListView特别相似,以下用一个Demo来说明: 1.Xcode中新建projectTestSimpleTableViewproject 2.在Main.storyboard中拖入一个UITableView控件 3.在ViewController.h文件里,实现UITableViewDelegate和UITableViewDataSource协议 这里须要说下的是.为了给UITable

IOS开发之UITableView使用大全。

前言: UITableView是ios开发中最常用的控件之一,几乎所有的应用都要用到,tableview继承UIScrollView,因此它不仅可以显示多行数据,而且具有scrollview的一些操作功能,比如滑动,自动偏移等等,因此非常强大. 而且tableview采用了数据源模式,因此只需要更改它的数据源,即可实现tableview显示数据的变化,而且tableviewcell还具有复用性. 所以tableview是一个在显示大量数据时及其好的选择. 1. tableview采用的是代理模式

iOS开发之UITableView使用总结

什么是UITableView 在众多移动应用中,能看到各式各样的表格数据 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳 UITableView的两种样式 UITableViewStylePlain UITableViewStyleGrouped tableView展示数据的过程 1.调用数据源的下面方法得知一共有多少组数据 - (NSInteger)numberOfSections

iOS开发之UITableView及cell重用

1.UITanleview有的两种风格 一种是Plain,一种是Grouped,可以从这里设置风格: 他们样式分别如下: Plain: Grouped: 2.tableView展示数据的过程: (1)首先,控制器要遵守UITableViewDataSource协议 @interface ViewController () <UITableViewDataSource> (2)调用数据源的下面方法得知一共有多少组数据 - (NSInteger)numberOfSectionsInTableVie

4、iOS 开发之 UITableView

一.UITableView的创建 表格控件在创建时必须指定样式,只能使用以下实例化方法 [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; UITableView的两种样式 1> UITableViewStylePlain 2> UITableViewStyleGrouped 2.UITableView的常见属性 // 头部视图(广告) @property (nonatomic,

IOS开发之UITableView

UITableView就相当于android中的listview,在这里先介绍一个简单的UItableView的用法,因为我也是刚学.而且也遇到了一些问题.直接上代码吧 这是ViewController.h  #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (nonatomic,retai

iOS开发之UITableView的滚动优化以及隐藏特性的使用

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源码传送:http://dwz.cn/Nret1 影响 UITableView 滚动的流畅性的原因 1. 在代理方法中做了过多的计算占用了 UI

李洪强iOS开发之RunLoop的原理和核心机制

李洪强iOS开发之RunLoop的原理和核心机制 搞iOS之后一直没有深入研究过RunLoop,非常的惭愧.刚好前一阵子负责性能优化项目,需要利用RunLoop做性能优化和性能检测,趁着这个机会深入研究了RunLoop的原理和特性. RunLoop的定义 当有持续的异步任务需求时,我们会创建一个独立的生命周期可控的线程.RunLoop就是控制线程生命周期并接收事件进行处理的机制. RunLoop是iOS事件响应与任务处理最核心的机制,它贯穿iOS整个系统. Foundation: NSRunLo