单元格的选中效果和辅助效果
2014-10-30 10:15
113 查看
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;
}
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;
}
相关文章推荐
- jquery table 鼠标选中单元格效果
- 取消记录tableView选中效果
- iOS 单元格选中之后返回的时候不再显示选中状态
- Vuejs实现列表选中效果
- 改变android的listview中选中时的效果
- 让表格的单元格不被选中的方法
- Windows 10 UWP开发:如何去掉ListView默认的选中效果
- Android gallery实现选中放大的效果
- SWT Table:获取表格所选中单元格所在行和列
- C# 鼠标悬停在datagridview的某单元格,显示悬浮框效果
- AndroidTV中利用MainUpView实现飞框选中效果
- gridview选中效果的实现,选中的改变背景。同时只有一个能被选中
- UITableView设置单元格选中后只显示一个打勾的三种简单方法(仅供参考)
- 简单的jquery左侧导航栏和页面选中效果
- ASP.NET gridview中点击任意行的任意位置选中某行交替效果 变换鼠标手势,获取选定行的指写数据
- 取消tableView选中效果
- jsp实现一个单元格对应多个单元格的效果
- Jquery Easy-UI中的Datagrid实现onclickrow无效果,onDblClickRow选中当前行
- easyui datagrid单元格实现溢出文本显示省略号的效果。
- DataGridView选中某个单元格中时自动选中当前行