美文网首页
单选/多选

单选/多选

作者: wpf_register | 来源:发表于2016-08-04 14:40 被阅读82次

面试的时候,让我用TableView写个多选框
<Fuck> 我没写出来!</Fuck>

今天抽空敲了下多选/单选,算是对一个多月前那次面试的画一个圆满的句号。
两种方法对应不同的需求。

代理实现

#import <UIKit/UIKit.h>
@class TableViewCell;

@protocol TableViewCellDelegate <NSObject>
@optional
//代理方法
- (void)selecterObj:(TableViewCell* )cell with:(NSInteger)type ;
@end


@interface TableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *selectedButton;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (nonatomic,assign) id<TableViewCellDelegate>delegate;
@end

//自定义的Cell
@implementation TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.selectedButton addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)changeImage:(UIButton*)sender{
    //button的点击只更换图片,其它由代理执行
    if(sender.isSelected == YES){
        sender.selected = NO;
        [sender setImage:[UIImage imageNamed:@"payUnChoose"] forState:UIControlStateNormal];
        [self.delegate  selecterObj:self with:0 ];

        
    }else{
        sender.selected = YES;
        [sender setImage:[UIImage imageNamed:@"payChoose"] forState:UIControlStateNormal];

        [self.delegate selecterObj:self with:1];
        
    }
}

tableView所在VC页面

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;//设置代理

  //为避免重用导致的错误
   if ([self.array containsObject:indexPath]) {
        [cell.selectedButton setImage:[UIImage imageNamed:@"Star-11"] forState:UIControlStateNormal];
    }else{
        [cell.selectedButton setImage:[UIImage imageNamed:@"Star-11y"] forState:UIControlStateNormal];
    }
    return cell;  
}

//代理方法
- (void)selecterObj:(TableViewCell *)cell with:(NSInteger)type {
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    if (type == 1) {
#ifdef singleChoose
        //单选时,先清空原来的数据,并将其button的selected置NO
        //多选时,则不需要清空数据,直接添加
        TableViewCell *tableViewCell = [self.tableView cellForRowAtIndexPath:self.array.lastObject];
        tableViewCell.selectedButton.selected = NO;
        [self.array removeLastObject];
#endif
        [self.array addObject:indexPath];     
    }else{
        
        [self.array removeObject:indexPath];
         cell.selectedButton.selected = NO;
    }
    [self.tableView reloadData];

}

系统原生

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView *tableView;
@end

@implementation ViewController

- (UITableView*)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];   
    }
    return _tableView;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 40;
}
//单元格
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    return cell;
}
//是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
/*************** 编辑样式*****************/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //这样可以实现多选并导致commitEditingStyle方法失效
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}

/*************** lifeCycle*****************/
- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"FUCK" style:UIBarButtonItemStyleDone target:self action:@selector(goAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"NOO" style:UIBarButtonItemStyleDone target:self action:@selector(gogoAction:)];
}
//编辑/完成
- (void)goAction:(UIBarButtonItem*)sender{
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"FUCK"]) {
        [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
        [self.tableView setEditing:YES animated:YES];
    }else{
        self.navigationItem.rightBarButtonItem.title = @"FUCK";
        [self.tableView setEditing:NO animated:YES];
    }
}
//得到点击的Cell
- (void)gogoAction:(UIBarButtonItem*)sender{
    NSMutableArray *array = [NSMutableArray array];
    for (NSIndexPath *indexPath in [self.tableView indexPathsForSelectedRows]) {
        [array addObject:indexPath];
    }
 }

相关文章

网友评论

      本文标题:单选/多选

      本文链接:https://www.haomeiwen.com/subject/uobcsttx.html