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
  • 相关阅读:
    企业微信中如何屏蔽另外一个企业的消息
    国际商标注册里面的头头道道
    如何使用charles,常见的charles功能
    Win8中GridVIew千变万化——绑定分组数据
    Win8中ListBox的ScrollIntoView方法失效
    一起零基础通过Nosql考试~~
    Ubuntu部署ros小龟爬爬实例
    学习算法是件怎样的事
    Centos7配置Python3.10.1
    基于Centos7、PHP7.4环境部署wordpress
  • 原文地址:https://www.cnblogs.com/Mgs1991/p/5138859.html
Copyright © 2011-2022 走看看