代码改变世界

TableView的复选功能

2016-01-18 12:41  000aaa  阅读(229)  评论(0编辑  收藏  举报
#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *selectArr;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _selectArr = [NSMutableArray arrayWithCapacity:0];
}
-(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 = @"FXCELLID";
    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 *button = (UIButton *)[cell viewWithTag:10];
    [button setImage:[UIImage imageNamed:@"checkbox_normal"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(cellButtonClick:forEvent:) forControlEvents:UIControlEventTouchUpInside];
    for (NSIndexPath *item in self.selectArr)
    {
        if (item.row == indexPath.row)
        {
            [button setImage:[UIImage imageNamed:@"checkbox_checked"] forState:UIControlStateNormal];
        }
    }
    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)cellButtonClick:(UIButton *)sender forEvent:(UIEvent *)event
{
    NSSet *touchs = [event allTouches];
    UITouch *touch = [touchs anyObject];
    CGPoint touchPoint = [touch locationInView:self.tableView];
    NSIndexPath *currentIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
    if (![self.selectArr containsObject:currentIndexPath])
    {
        [self.selectArr addObject:currentIndexPath];
    }
    else
    {
        [self.selectArr removeObject:currentIndexPath];
    }
    [_tableView reloadRowsAtIndexPaths:@[currentIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (IBAction)selectAll:(UIButton *)sender
{
    for (int i = 0; i < 30; i++)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [self.selectArr addObject:indexPath];
    }
    [_tableView reloadData];
}
- (IBAction)cleanUp:(UIButton *)sender
{
    [self.selectArr removeAllObjects];
    [_tableView reloadData];
}
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end