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;
    
        
        
    }
  • 相关阅读:
    C# Net Core 使用 ClientWebSocket 实现 WebSocket 客户端
    C# Net 使用 RSA 加密解密 OpenSSL 生成的密码
    VS 代码提示默认不选中 解决办法
    C# While 超时设置
    C# 比较日期格式中的年月大小
    C#实现QQ邮箱IMAP之邮件处理
    Windwos服务之定时发送邮件(一)
    js基于“合成大西瓜的”碰撞模型(一)
    Windows下,通过运行直接打开软件
    C#爬取国家统计局五级地址
  • 原文地址:https://www.cnblogs.com/leeAsia/p/3342153.html
Copyright © 2011-2022 走看看