美文网首页
iOS学习之 UITableViewCell的单选

iOS学习之 UITableViewCell的单选

作者: 鑫飞 | 来源:发表于2019-04-10 16:45 被阅读0次

最近在做一个页面关于UITableViewCell的单选,就是选中某行时后面有对勾或者是标记选中的那行,整理了一下,方便各大网友。下面就说下实现的方法。

第一种的实现

在.m的文件里:

#import "MyViewController.h"
#import "NextViewController.h"

@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, assign)BOOL ifSelected;//是否选中
@property (nonatomic, strong)NSIndexPath * lastSelected;//上一次选中的索引

@end

在viewDidLoad里面的实现:

self.ifSelected = NO;//是否被选中 默认为NO

在TableView数据源方法里面:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellIndentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"男";
    }else{
        cell.textLabel.text = @"女";
    }
    if (self.ifSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

在tableview代理方法里面:

#pragma mark -tableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath * temp = self.lastSelected;//暂存上一次选中的行
    if (temp && temp != indexPath)//如果上一次的选中的行存在,并且不是当前选中的这一行,则让上一行不选中
    {
        self.ifSelected = NO;//修改之前选中的cell的数据为不选中
        [tableView reloadRowsAtIndexPaths:@[temp] withRowAnimation:UITableViewRowAnimationAutomatic];//刷新该行
    }
    self.lastSelected = indexPath;//选中的修改为当前行
    self.ifSelected = YES;//修改这个被选中的一行
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//重新刷新
}

实现的效果图:

20151126164004744.png

第二种实现方法

.m文件里面:

#import "AdViewController.h"
#import "NextViewController.h"

@interface AdViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;

@end

在TableView数据源里面的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellIndentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    if ([self.selectedIndexPath isEqual:indexPath]){

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    }else{

        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

在tableView代理方法里面:

#pragma mark -tableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectedIndexPath) {

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];

        cell.accessoryType = UITableViewCellAccessoryNone;

    }

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    self.selectedIndexPath = indexPath;
}

感谢:
https://blog.csdn.net/qq_29284809/article/details/50057989

相关文章

网友评论

      本文标题:iOS学习之 UITableViewCell的单选

      本文链接:https://www.haomeiwen.com/subject/ypzpcqtx.html