单元格的选中效果和辅助效果
来源:互联网 发布:山东省软件协会 编辑:程序博客网 时间:2024/04/29 21:43
AppDelegate设置根视图控制器
RootViewController.h
@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>@property(nonatomic, retain)NSArray *data;@property(nonatomic, retain)NSIndexPath *selectIndexPath; //标记选中的单元格
RootViewController.m
- (void)viewDidLoad{ [super viewDidLoad]; //创建表视图 UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain]; tableView.dataSource = self; tableView.delegate = self; //将表视图添加到视图上显示 [self.view addSubview:tableView]; [tableView release]; _data = [[UIFont familyNames] retain]; }#pragma mark - UITableView dataSource//返回需要创建多少个单元格- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _data.count;}//创建cell的,调用的次数由单元格的个数决定- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //使用static修饰避免了重复创建字符串 static NSString *iden = nil; if (indexPath.row == 0) { iden = @"cell_head"; } else if (indexPath.row == _data.count - 1) { iden = @"cell_last"; } else { iden = @"cell_common"; } //从闲置池中查找是否存在可以使用的单元格 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden]; //如果没有找到则创建 if (cell == nil && indexPath.row == 0) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease]; cell.textLabel.backgroundColor = [UIColor clearColor]; //设置正常状态显示的图片 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_head"]]; cell.backgroundView = imageView; [imageView release]; //设置选中后显示的额图片 UIImageView *selecteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_head_tapped"]]; cell.selectedBackgroundView = selecteImg; [selecteImg release]; //设置选中后label显示的字体 cell.textLabel.highlightedTextColor = [UIColor redColor]; } else if (cell==nil && indexPath.row == _data.count-1) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease]; cell.textLabel.backgroundColor = [UIColor clearColor]; //设置正常状态显示的图片 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_bottom"]]; cell.backgroundView = imageView; [imageView release]; //设置选中后显示的额图片 UIImageView *selecteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_bottom_tapped"]]; cell.selectedBackgroundView = selecteImg; [selecteImg release]; //设置选中后label显示的字体 cell.textLabel.highlightedTextColor = [UIColor redColor]; } else if(cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease]; cell.textLabel.backgroundColor = [UIColor clearColor]; //设置正常状态显示的图片 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_common"]]; cell.backgroundView = imageView; [imageView release]; //设置选中后显示的额图片 UIImageView *selecteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_common_tapped"]]; cell.selectedBackgroundView = selecteImg; [selecteImg release]; //设置选中后label显示的字体 cell.textLabel.highlightedTextColor = [UIColor redColor]; } if (indexPath.row == self.selectIndexPath.row && self.selectIndexPath != nil) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } /* //设置第一个单元格显示的图片 if (indexPath.row == 0) { //设置正常状态显示的图片 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_head"]]; cell.backgroundView = imageView; [imageView release]; //设置选中后显示的额图片 UIImageView *selecteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_head_tapped"]]; cell.selectedBackgroundView = selecteImg; [selecteImg release]; }else if (indexPath.row == _data.count - 1) { //设置正常状态显示的图片 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_bottom"]]; cell.backgroundView = imageView; [imageView release]; //设置选中后显示的额图片 UIImageView *selecteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_bottom_tapped"]]; cell.selectedBackgroundView = selecteImg; [selecteImg release]; }else { //设置正常状态显示的图片 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_common"]]; cell.backgroundView = imageView; [imageView release]; //设置选中后显示的额图片 UIImageView *selecteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCell_common_tapped"]]; cell.selectedBackgroundView = selecteImg; [selecteImg release]; } */ //给cell添加显示的数据 cell.textLabel.text = [_data objectAtIndex:indexPath.row]; return cell;}#pragma mark - UITableView delegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //取消上一次选中的额单元格 if (self.selectIndexPath != nil) { UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:self.selectIndexPath]; lastCell.accessoryType = UITableViewCellAccessoryNone; } //查找选中的cell,并且给它添加选中的标记 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; //记录选中的单元格 self.selectIndexPath = indexPath;}
0 0
- 单元格的选中效果和辅助效果
- jquery table 鼠标选中单元格效果
- 仿按钮效果的单元格
- CCMenuItemToggle实现选中的效果
- ListView的固定选中效果
- tabBarItem的选中抖动效果
- 自定义tableviewCell的选中效果
- JTable 为单元格添加按钮效果和事件效果
- JTable 为单元格添加按钮效果和事件效果
- JTable 为单元格添加按钮效果和事件效果
- TD单元格背景颜色的变换效果
- 如何去掉UITableView的单元格点击效果
- jsp实现一个单元格对应多个单元格的效果
- Android ListView自定义和选中效果
- Android ListView自定义和选中效果
- Android ListView自定义和选中效果
- checkbox 选中和取消切换没效果
- 选中文字高亮的css效果
- vs2010快捷键
- 算法导论-第10章-10.4 二叉树之基础篇
- STVD TIM4 8位中断定时器
- 使用 JMeter 完成常用的压力测试
- Perl—— 文件介绍
- 单元格的选中效果和辅助效果
- 陈彤离职,新浪在门户竞争中将继续被边缘化
- hdfs常用命令
- 在线jpg转换成pdf格式
- Mysql之union联合查询
- response.setContentType()的作用及参数
- NYOJ 1100 广搜优先队列
- u-boot: Error: Can't overwrite "ethaddr"
- Android生成SDK及代码混淆