代码改变世界

TableView checkBox 单选功能

2016-01-18 12:39  000aaa  阅读(383)  评论(0编辑  收藏  举报
#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