zoukankan      html  css  js  c++  java
  • UITableView accessoryType single check

     做了一个单选勾选的表之前有些问题,修改了一下

    .h文件
    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
    {
    
        NSArray *arrry;
        NSInteger checkedIndexPath;  
    
    }
    @end
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UITableView *atable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 350)];
        atable.delegate=self;
        atable.dataSource=self;
        checkedIndexPath=-1;
        [self.view addSubview:atable];
        arrry=[[NSArray alloc] initWithObjects:@"1",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2", nil];
        
    }
    
    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *cellinde=@"cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellinde];
        if (cell==nil) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellinde];
        }
    
    
        cell.accessoryType=UITableViewCellAccessoryNone;
    
        
        cell.textLabel.text=[arrry objectAtIndex:indexPath.row];
        if (checkedIndexPath==indexPath.row) {
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }
    
    return cell;
    
    
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    
    
        return [arrry count];
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        if (checkedIndexPath)
        {
            if (indexPath.row==checkedIndexPath) return;
            UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:checkedIndexPath inSection:0]];
            [uncheckCell setAccessoryType:UITableViewCellAccessoryNone];
        }
        
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        checkedIndexPath = indexPath.row;
    
        
        
    }
  • 相关阅读:
    OData – Query to Expression
    ASP.NET Core Library – Hangfire
    ASP.NET Core Library – Nager.PublicSuffix
    EF Core – Unit of Work, DbContext, Transaction 概念解释
    ASP.NET Core – Program.cs and Startup.cs 小笔记
    OData – OData vs GraphQL
    Fluent Builder 模式
    C# – 冷知识 (新手)
    ASP.NET Core Library – scriban (Template Engine)
    The "蛋炒饭" in microsoft must cost you ten yuan RMB!
  • 原文地址:https://www.cnblogs.com/leeAsia/p/3342153.html
Copyright © 2011-2022 走看看