iOS学习笔记8-UITableView的定制
来源:互联网 发布:淘宝好评返现违规补救 编辑:程序博客网 时间:2024/05/23 22:47
UITableView在iOS的工程应用中,扮演着非常重要的角色,今天利用前一个demo继续做UITableView的介绍。
UITableView实现表格绘制,其中几个重要的方法,需要拿出来特别梳理:
1.使用UITableView进行表格显示时,需要设置UITableView的dataSource属性及其delegate属性,然后分别实现UItableViewDataSource协议和UItableViewDelegate协议的相关方法;
//tableView的定制 UITableView *setTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height - 44)style:UITableViewStyleGrouped]; setTableView.backgroundColor = [UIColor clearColor]; setTableView.dataSource = self; setTableView.delegate = self; [self.view addSubview:setTableView];2.几个常用方法:
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{}
此方法返回有多少段
//定义tableView- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}
例子中的效果图:
(2)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}
此方法返回每段中单元格数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //此处是想说明每段中返回的单元格数量,所以加了个else方法,如果想看else后面的效果,可以把上面- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView方法返回值改成2,以查看 if (section==0) { return 5; }else{ return 4; }}(3)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}
此方法是对每个单元格(及cell)的定制
就cell定制时,查看官方文档可知,cell共有四种风格,下面的demo中,我将四种风格都一一定制了出来:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Identifier = @"Identifier"; // Used by the delegate to acquire an already allocated(fenpei) cell, in lieu(replace) of allocating a new one. //Identifier是标记,dequeueReusableCellWithIdentifier方法是当有可重用的cell时,根据标记直接取,而不用重新生成,可以提高效率。 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Identifier]; } if (indexPath.section == 0 ) { cell.backgroundColor = [UIColor orangeColor]; cell.textLabel.textColor = [UIColor whiteColor]; if (indexPath.row == 0) { cell.textLabel.text = @"firstCell"; cell.textLabel.textColor = [UIColor whiteColor]; cell.textLabel.textAlignment = NSTextAlignmentCenter; //cell的第二种风格 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // cell.imageView.image = [UIImage imageNamed:@"about_tag"]; }else if (indexPath.row == 1) { cell.textLabel.text = @"secondCell"; cell.textLabel.textColor = [UIColor whiteColor];// cell.textAlignment = UITextAlignmentCenter; cell.textLabel.textAlignment = kCTCenterTextAlignment; cell.detailTextLabel.text = @"--2"; cell.detailTextLabel.textColor = [UIColor whiteColor]; cell.detailTextLabel.textAlignment = NSTextAlignmentCenter; cell.detailTextLabel.font = [UIFont systemFontOfSize:16]; cell.detailTextLabel.textColor = [UIColor whiteColor]; cell.imageView.image = [UIImage imageNamed:@"version_tag"]; }else if(indexPath.row == 2) { cell.textLabel.text = @"thirdCell"; cell.textLabel.textColor = [UIColor whiteColor]; cell.textLabel.textAlignment = NSTextAlignmentCenter; cell.detailTextLabel.text = @"--3"; cell.detailTextLabel.textColor = [UIColor whiteColor]; //cell的第三种风格 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; cell.imageView.image = [UIImage imageNamed:@"introduce_tag"]; }else if(indexPath.row ==3) { cell.textLabel.text = @"fourthCell"; cell.textLabel.textColor=[UIColor whiteColor]; cell.textLabel.textAlignment = NSTextAlignmentCenter; cell.detailTextLabel.text = @"--4"; cell.detailTextLabel.textColor = [UIColor whiteColor]; cell.imageView.image = [UIImage imageNamed:@"about_tag"]; //cell的第一种风格 cell.accessoryType = UITableViewCellAccessoryNone; }else if (indexPath.row ==4) { cell.textLabel.text = @"fiveCell"; cell.textLabel.textAlignment = NSTextAlignmentCenter; cell.textLabel.textColor = [UIColor whiteColor]; cell.detailTextLabel.text = @"---5"; cell.detailTextLabel.textColor = [UIColor whiteColor]; //cell的第四种风格 cell.accessoryType = UITableViewCellAccessoryCheckmark; cell.imageView.image = [UIImage imageNamed:@"about_tag"]; } }else if (indexPath.section ==1) { cell.backgroundColor = [UIColor cyanColor]; if (indexPath.row == 0) { cell.textLabel.text = @"2-1"; cell.textLabel.textColor = [UIColor whiteColor]; cell.accessoryType = UITableViewCellAccessoryNone; }else if(indexPath.row ==1){ cell.textLabel.text =@"2-2"; cell.textLabel.textColor = [UIColor whiteColor]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; }else if(indexPath.row ==2) { cell.textLabel.text = @"2-3"; cell.textLabel.textColor = [UIColor whiteColor]; cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; }else if(indexPath.row ==3 ) { cell.textLabel.text = @"2-4"; cell.textLabel.textColor = [UIColor whiteColor]; cell.accessoryType = UITableViewCellAccessoryCheckmark; } } return cell; }
(4)-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}
此方法是处理每个cell被选择后的动作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { if (indexPath.row == 0) { [self performSelector:@selector(cancelAction)]; }else if (indexPath.row == 1) { [self performSelector:@selector(cancelAction)]; }else if (indexPath.row == 2) { [self performSelector:@selector(cancelAction)]; }else if (indexPath.row == 3) { [self performSelector:@selector(cancelAction)]; }else if (indexPath.row == 4) { [self performSelector:@selector(cancelAction)]; }}else if (indexPath.section == 1) { }}//选择器触发- (void)cancelAction{ NSString *message = @"你已经点击"; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"点击事件" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; alert.frame=CGRectMake(0, 0, 80, 80); alert.center = self.view.center; [alert show]; }
(5)在定制cell时候,cell的第三种风格UITableViewCellAccessoryDetailDisclosureButton,如图:
那怎么为UITableViewCellAccessoryDetailDisclosureButton定制事件呢?查看文档可知,可以通过下面的方法定制:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
//怎么为 UITableViewCellAccessoryDetailDisclosureButton 定义事件,发现在UITableViewDelegate 可以定义这个按钮的事件- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ NSInteger row = indexPath.row; if (row == 2) { NSLog(@"点击了cell右边按钮"); // }}demo下载
- iOS学习笔记8-UITableView的定制
- 方法定制iOS学习笔记8-UITableView的定制
- 【ios学习记录】-如何定制UITableView的圆角单元格
- IOS UITableView 学习笔记
- iOS 学习笔记 uitableView
- xamarin学习笔记B07(ios的UITableView)
- iOS学习笔记10-UITabBarController的定制
- iOS开发学习笔记 UITableview
- 转发iOS UITableView学习笔记
- iOS学习笔记03-UITableView
- iOS学习笔记—— UItableView 控件的简单使用
- IOS学习笔记-UITableView表索引的颜色设置
- iOS学习笔记:UITableView想要的都有
- iOS学习笔记-026.UITableView的基本用法
- iOS学习笔记-028.UITableView的省市主从表
- iOS学习笔记-029.UITableView的编辑模式
- ios学习笔记之-UITableView的分组显示
- 定制UITableView的单元格
- SQLServer学习记录
- Android之实现手势缩放ImageView
- hibernate中save,update,saveOrUpdate与三大状态的关系
- 关于使用Flexigrid的问题
- ApplicationDomain学习
- iOS学习笔记8-UITableView的定制
- 读书笔记:视图和模板,改用Handlerbars做演示案例
- IOS6之AutoLayout(一)
- GO 语言学习
- java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory的解决
- 程序员_Java基础之<十二>-IO流<3>其他流对象、编码
- Z26上的维吉尼亚密码体制
- rqnoj-479
- MFC TCHAR 和CHAR相互转换