首页 > 移动平台 > 详细

ios开发之--tableview单选实现

时间:2018-03-19 13:06:25      阅读:505      评论:0      收藏:0      [点我收藏+]

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

直接上代码:

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单选实现

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!