zoukankan      html  css  js  c++  java
  • TableView checkBox 单选功能

    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    {
        NSIndexPath * m_LastIndexPath;
        NSIndexPath * m_currentIndexPath;
    }
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 30;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       static NSString *ID = @"SINGLECELL";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        ((UILabel *)[cell viewWithTag:20]).text = [NSString stringWithFormat:@"第%ld行",indexPath.row+1];
        UIButton *cellButton = (UIButton *)[cell viewWithTag:10];
        
        if (m_LastIndexPath && m_currentIndexPath && m_LastIndexPath == m_currentIndexPath)
        {
            m_currentIndexPath = nil;
            m_LastIndexPath = nil;
        }
        [cellButton setImage:[UIImage imageNamed:@"Choice_icon2"] forState:UIControlStateNormal];
        
        if (m_currentIndexPath && m_currentIndexPath.row == indexPath.row)
        {
            [cellButton setImage:[UIImage imageNamed:@"Choice_icon"] forState:UIControlStateNormal];
        }
        if (m_LastIndexPath && m_LastIndexPath != m_currentIndexPath && m_LastIndexPath.row == indexPath.row )
        {
             [cellButton setImage:[UIImage imageNamed:@"Choice_icon2"] forState:UIControlStateNormal];
        }
       
        [cellButton addTarget:self action:@selector(cellButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"你选中的是第%ld行",indexPath.row+1] preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"do anything you like");
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    -(void)cellButton:(UIButton *)sender forEvent:(UIEvent *)event
    {
        NSSet *touchs = [event allTouches];
        UITouch *touch = [touchs anyObject];
        CGPoint touchPoint = [touch locationInView:self.tableView];
        m_currentIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
        
        if (m_LastIndexPath == m_currentIndexPath)
        {
            [_tableView reloadRowsAtIndexPaths:@[m_currentIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        else
        {
            if (m_LastIndexPath)
            {
                [_tableView reloadRowsAtIndexPaths:@[m_LastIndexPath,m_currentIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
            }
            else
            {
                [_tableView reloadRowsAtIndexPaths:@[m_currentIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
            }
            m_LastIndexPath = m_currentIndexPath;
        }
    }
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    
    @end
  • 相关阅读:
    Linux记录-普通用户下执行sudo xxx 找不到命令解决方案
    Hadoop记录-MRv2(Yarn)运行机制
    The difference between Severity and Priority
    1.3 Seven Testing Principles
    [转]ISTQB FL初级认证考试资料(中文)
    ISTQB测试人员认证 初级(基础级)大纲
    自定义控件VS用户控件
    进程、线程与应用程序域
    [转]软件测试演义——中高级系列(序)
    [转]在做自动化测试之前你需要知道的
  • 原文地址:https://www.cnblogs.com/Mgs1991/p/5138859.html
Copyright © 2011-2022 走看看