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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值