码迷,mamicode.com
首页 > 移动开发 > 详细

ios开发之--tableview单选实现

时间:2018-03-19 13:46:31      阅读:453      评论:0      收藏:0      [点我收藏+]

标签:led   def   效果图   seq   inf   tab   setvalue   name   class   

实现思路比较简单,这里仅做记录:

直接上代码:

1,实现didSelectRowAtIndexPath方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSUserDefaults standardUserDefaults]setValue:[array objectAtIndex:indexPath.row] forKey:APP_CHANGEVOICE];
    
    [_sextTableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

在cellForRowAtIndexPath里面实现方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellTableIdentifier = @"CellTableIdentifier";
    hPublickCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil)
    {
        cell = [[hPublickCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellTableIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.textsLabel.text = array[indexPath.row];
    cell.selectionStyle=UITableViewCellSelectionStyleGray;
    //选择状态的存储
    if ([[[NSUserDefaults standardUserDefaults]valueForKey:APP_CHANGEVOICE] isEqualToString:[array objectAtIndex:indexPath.row]])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
    
}

这里面的array是数据源数组。效果图如下:

技术分享图片

 

 

2,上面这种是系统的选中样式,下面是自定义的:

代码如下:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShippingAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShippingAddressCell"];
    if (!cell) {
        cell = [[ShippingAddressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ShippingAddressCell"];
    }
    
    if (self.lastIndexPath == indexPath)
    {
        cell.selectedImg.image = [UIImage imageNamed:@"clicked"];
    }else
    {
        cell.selectedImg.image = [UIImage imageNamed:@"unClick"];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //之前选中的,取消选择
    ShippingAddressCell *celled = [tableView cellForRowAtIndexPath:_lastIndexPath];
    celled.selectedImg.image = [UIImage imageNamed:@"unClick"];
    //记录当前选中的位置索引
    _lastIndexPath = indexPath;
    //当前选择的打勾
    ShippingAddressCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectedImg.image = [UIImage imageNamed:@"clicked"];
}

这样就可以实现了,截图如下:

技术分享图片

 

多选的有空再完善!代码可以直接粘贴使用!

 

ios开发之--tableview单选实现

标签:led   def   效果图   seq   inf   tab   setvalue   name   class   

原文地址:https://www.cnblogs.com/hero11223/p/8601038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!