您的位置:首页 > 其它

单元格的选中效果和辅助效果

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息