好久沒更新博客了,今天寫了一個自定義的多選列表,可以跟愛學習的各位進行分享,首先我們先來看一下效果圖:
一般大家都是用UITableView自己的編輯模式來實現CheckBox的,這裏我們用自定義Cell和兩張圖片來實現,一張是未選中,一張是選中的圖片
好了,我們首先來看一下代碼:首先在Cell中定義了三個控件,兩個UILabel和一個UIImageView
- #import <UIKit/UIKit.h>
- @interface MultiChoceCell : UITableViewCell
- @property (strong, nonatomic) IBOutlet UILabel *nameLabel;
- @property (strong, nonatomic) IBOutlet UILabel *departLable;
- @property (strong, nonatomic) IBOutlet UIImageView *checkBox;
- @end
好了,這個相信各位學過UITableViewCell的同學都應該知道,接下來,我們就來寫最主要的ViewController了
在頭文件.h文件裏,我們首先定義了一個協議專門用來做回調的,還定義了一個選項數組和選中的數組,還有一個UITableView,我個人喜歡在ViewController裏套上UITableView,因爲可以改變TableView的大小。
- #import <UIKit/UIKit.h>
- @class MultiChoceViewController;
- @protocol MultiChoceDelegate <NSObject>
- @required
- -(void)MultiChoceSelectArray:(NSArray *)array ViewController:(MultiChoceViewController *)controller;
- @end
- @interface MultiChoceViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
- @property(nonatomic, strong)NSArray *itemArray;
- @property(nonatomic, strong)NSMutableArray *selectArray;
- @property (strong, nonatomic) IBOutlet UITableView *mTableView;
- @property (nonatomic, strong) id<MultiChoceDelegate> delegate;
- - (IBAction)backAction:(id)sender;
- - (IBAction)okAction:(id)sender;
- @end
- #import "MultiChoceViewController.h"
- #import "MultiChoceCell.h"
- @interface MultiChoceViewController ()
- @end
- @implementation MultiChoceViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [_mTableView setBackgroundColor:[UIColor clearColor]];
- // Do any additional setup after loading the view from its nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return [_itemArray count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *identifier = @"itemCell";
- MultiChoceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
- if (cell == nil) {
- NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MultiChoceCell" owner:self options:nil];
- cell = [array objectAtIndex:0];
- }
- NSDictionary *dict = [_itemArray objectAtIndex:indexPath.row];
- cell.nameLabel.text = [dict objectForKey:@"UserName"];
- cell.departLable.text = [dict objectForKey:@"DepartMent"];
- if ([_selectArray containsObject:dict]) {
- cell.checkBox.image = [UIImage imageNamed:@"checked.png"];
- }
- return cell;
- }
- - (IBAction)backAction:(id)sender {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (IBAction)okAction:(id)sender {
- [_delegate MultiChoceSelectArray:_selectArray ViewController:self];
- }
- #pragma mark UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- NSDictionary *selectDict = [_itemArray objectAtIndex:indexPath.row];
- //判斷數據是否在選擇列表中
- if ([_selectArray containsObject:selectDict]) {
- [_selectArray removeObject:selectDict];
- }else{
- [_selectArray addObject:selectDict];
- }
- [_mTableView reloadData];
- }
- @end
在調用的頁面,我們把itemArray和selectArray傳入
- MultiChoceViewController *controlller = [[MultiChoceViewController alloc] initWithNibName:@"MultiChoceViewController" bundle:nil];
- controlller.delegate = self;
- controlller.itemArray = userArray;
- controlller.selectArray = selectArray;
- [self.navigationController pushViewController:controlller animated:YES];
- #pragma mark MultiChoceDelegate
- -(void)MultiChoceSelectArray:(NSMutableArray *)array ViewController:(MultiChoceViewController *)controller{
- selectArray = array;
- [self.navigationController popViewControllerAnimated:YES];
- }