iOS之tableView基本用法
2016-05-08 12:52
537 查看
</pre><p>@implementation ViewController- (void)viewDidLoad {<span style="font-size:12px"> [super viewDidLoad]; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -64 ) style:UITableViewStylePlain];//注意问题:tableStyle还有一个grouped类型,用这个会在tableView上方出现空白</span></p><p><span style="font-size:12px"> tableView.delegate = self; tableView.dataSource = self; //设置表头// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];// view.backgroundColor = [UIColor redColor]; //将一张图片作为表头 UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)]; imageV.image = [UIImage imageNamed:@"tu.jpg"]; tableView.tableHeaderView = imageV; [self.view addSubview:tableView]; </span> //分区 </p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span class="s1"> [</span><span class="s2">tableview </span>setSeparatorColor<span class="s1">:[</span>UIColor <span class="s1"></span>blueColor<span class="s1">]]; //设置分割线为蓝色</span></p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span class="s1"></span></p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">隐藏UITableViewCell的分隔线</p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">[self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; </p><p class="p2" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"></p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> UITableViewCellSeparatorStyle有如下几种 </p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span class="s1">typedef</span> <span class="s2">NS_ENUM</span>(NSInteger, UITableViewCellSeparatorStyle) {</p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> UITableViewCellSeparatorStyleNone,</p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> UITableViewCellSeparatorStyleSingleLine,</p><p class="p2" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span class="s3"> UITableViewCellSeparatorStyleSingleLineEtched </span>// This separator style is only supported for grouped style table views currently</p><p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">};</p>}<p class="p1" style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"></p>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *indentifier = @"cell1"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier]; } cell.textLabel.text = @"分区和表头"; return cell;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //section代表分区 第一分区两行,其他分区3行 if (section == 0) { return 2; }else if(section== 1){ return 3; }else{ return 1; }// return 5;//每个分区都是5行}//分区个数-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3;}//分区高度-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 60;}//cell高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60;}//分区的标题-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (section == 0) { return @"A"; } else if(section == 1){ return @"B"; }else{ return @"C"; } }//自定义分区的样式-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)]; view.backgroundColor = [UIColor greenColor]; return view;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}<p>@end</p><p></p><pre name="code" class="objc">一个section刷新NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];//一个cell刷新NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone]; http://www.cnblogs.com/wendingding/p/3801454.html发现让tableview 滚动到顶部 这句话是最简单方便的了[myTB setContentOffset:CGPointMake(0,0) animated:NO];
//
相关文章推荐
- iOS开发中常用的几个插件
- ios基础知识--03
- Vesions ignore & ld: library not found for -l问题总结
- ios基础知识--02
- iOS开发之多XIB之间相互关联
- ios基础知识--01
- 【iOS】NSDate分类,获得中国农历
- iOS开发之多storyboard相互关联
- iOS本地数据存取,这里就够了
- IOS9新特性慢慢整理
- iOS后台持续播放音乐
- iOS学习笔记77-ios开发,javascript直接调用oc代码而非通过改变url回调方式
- iOS Runloop详解
- iOS图片的自动适配(通过类别实现)
- IOS开发学习-基础环境搭建-1
- iOS三种多线程编程的技术介绍
- iOS开发多线程篇—多线程的的相关概念(1)
- iOS多线程篇:NSThread
- ios中URL中出现字典参数的处理问题
- IOS 模仿TableView封装