美文网首页iOS DeveloperiOS 开发
单选 -- 点击显示“对号”功能

单选 -- 点击显示“对号”功能

作者: GF极客天涯 | 来源:发表于2016-09-05 16:04 被阅读280次
    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    @property (nonatomic,strong) UITableView * tableView;
    @property (nonatomic,assign) NSInteger currentIndex;
    @end
    static NSString *identifier = @"chooseCell";
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [ self loadSubview];
        // Do any additional setup after loading the view, typically from a nib.
    }
    // 加载视图的方法
    - (void)loadSubview
    {
        
        // *********************** 初始化tableView  ***************************
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 , 320, 560) style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor greenColor];
        
        // 设置代理
        self.tableView.delegate  = self;
        self.tableView.dataSource = self;
        // 注册cell
        [self.tableView  registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
        // 添加到根视图上
        [self.view addSubview:self.tableView];
        
    }
    #pragma mark - tableViewe的代理方法
    // 返回分区中cell的个数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 26;
    }
    // 返回分区的个数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    // 返回cell的方法
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle =  UITableViewCellAccessoryCheckmark; //选中cell时无色
        }
        cell.textLabel.text = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"].mutableCopy[indexPath.row];
        return cell;
    }
    
    
    
    - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
    {
        if(indexPath.row==_currentIndex){
            return UITableViewCellAccessoryCheckmark;
        }
        else{
            return UITableViewCellAccessoryNone;
        }
    }
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        if(indexPath.row==_currentIndex){
            return;
        }
        NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:_currentIndex
                                                       inSection:0];
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        if (newCell.accessoryType == UITableViewCellAccessoryNone) {
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            
        }
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
        if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            
        }
        _currentIndex=indexPath.row;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    Untitled.gif

    相关文章

      网友评论

        本文标题:单选 -- 点击显示“对号”功能

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