iOS - UITableView 多选功能实现

1:自定义Cell中的代码

#import <UIKit/UIKit.h>
@interface TestCell : UITableViewCell
@property(nonatomic,copy)NSString *content;
/**
 *  标记行是否被选中
 */
@property(nonatomic,assign)BOOL isChecked;
@end
#import "TestCell.h"
@interface TestCell()
@property (weak, nonatomic) IBOutlet UIImageView *mark_ImageView;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel;

@end

@implementation TestCell

- (void)awakeFromNib
{
    self.mark_ImageView.hidden = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

   
}
-(void)setContent:(NSString *)content
{
    if (_content!=content) {
        _firstLabel.text=content;
    }

}
/**
 *  这里只是用到了一张图所以通过来imageview的hidden来显示是否选中
 *
 *  @param isChecked
 */
- (void)setIsChecked:(BOOL)isChecked
{
    _isChecked = isChecked;
    //选中
    if (_isChecked) {
        self.mark_ImageView.hidden = NO;
        self.mark_ImageView.image = [UIImage imageNamed:@"choice"];
    }
    else
    {
        self.mark_ImageView.hidden = YES;
    }

}

@end


2:ViewController中的代码

#import "ViewController.h" #import "TestCell.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,strong)NSMutableArray *dataArray; /** * 保存选中行的对应的数据 */ @property(nonatomic,strong)NSMutableArray *markArray; /** * 保存标记选中行字典的数组 */ @property(nonatomic,strong)NSMutableArray *checkArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; for (NSInteger i=0; i<20; i++) { [self.dataArray addObject:[NSString stringWithFormat:@"选中第%ld行",i]]; } for (NSInteger i =0; i<self.dataArray.count; i++) { NSMutableDictionary *resultDict = [NSMutableDictionary dictionaryWithObject:@0 forKey:@"isChecked"]; [self.markArray addObject:resultDict]; } } - (NSMutableArray *)dataArray{ if (!_dataArray) { _dataArray =[NSMutableArray array]; } return _dataArray; } - (NSMutableArray *)markArray{ if (!_markArray) { _markArray =[NSMutableArray array]; } return _markArray; } - (NSMutableArray *)checkArray{ if (!_checkArray) { _checkArray =[NSMutableArray array]; } return _checkArray; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } static NSString *cellIdentifier = @"TestCell"; -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TestCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; cell.content= self.dataArray[indexPath.row]; NSDictionary *resultDict = self.markArray[indexPath.row]; cell.isChecked = [resultDict[@"isChecked"] boolValue]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44.0f; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { TestCell *cell = (TestCell *)[tableView cellForRowAtIndexPath:indexPath]; NSString *checkString = self.dataArray[indexPath.row]; NSMutableDictionary *checkedResult = self.markArray[indexPath.row]; //isChecked为NO则表明要把这行置为选中状态 if ([checkedResult[@"isChecked"] boolValue]==NO) { [checkedResult setValue:@1 forKey:@"isChecked"]; cell.isChecked = YES; [self.checkArray addObject:checkString]; } else{ [checkedResult setValue:@0 forKey:@"isChecked"]; cell.isChecked = NO; [self.checkArray removeObject:checkString]; } [tableView deselectRowAtIndexPath:indexPath animated:YES]; //刷新指定的行 NSIndexPath *indexPath_Row=[NSIndexPath indexPathForRow:indexPath.row inSection:0]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath_Row,nil] withRowAnimation:UITableViewRowAnimationNone]; NSLog(@"self.checkArray =%@",self.checkArray); } @end

posted @ 2016-08-26 16:32  曹县三胖暴打大猩猩  阅读(511)  评论(0编辑  收藏  举报