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
  • 相关阅读:
    OpenCV基本函数学习
    OpenCV:概述、结构和内容
    NS2典型例子简单分析
    NS2仿真过程中需要的语言及基本组件
    洛谷 P2393 yyy loves Maths II
    洛谷 P1922 女仆咖啡厅桌游吧
    洛谷 P2559 [AHOI2002]哈利·波特与魔法石
    洛谷 P1301 魔鬼之城
    洛谷 P2383 狗哥玩木棒
    洛谷 P2298 Mzc和男家丁的游戏
  • 原文地址:https://www.cnblogs.com/Mgs1991/p/5138859.html
Copyright © 2011-2022 走看看