美文网首页
UITabelView多选和联系人排序

UITabelView多选和联系人排序

作者: LennonLin | 来源:发表于2016-11-03 21:28 被阅读67次

一: 实现系统的多选:

  • 效果如下图:
屏幕快照 2016-11-03 下午9.25.30.png
#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataMutableArray; // 数据源

@end

@implementation ViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getDataSource];
    [self addSubViewSetSize];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(tableViewEdit)];
}

#pragma mark - Custom Accessors;

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewScrollPositionNone;
        _tableView.allowsMultipleSelection = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}

- (NSMutableArray *)dataMutableArray {
    if (!_dataMutableArray) {
        _dataMutableArray = [NSMutableArray array];
    }
    return _dataMutableArray;
}

#pragma mark - private

- (void)addSubViewSetSize {
    
    [self.view addSubview:self.tableView];
}

#pragma mark - action

- (void)tableViewEdit {
    
    
    if (!_tableView.editing) {
        [_dataMutableArray removeAllObjects];
        self.navigationItem.rightBarButtonItem.title = @"完成";
        _tableView.editing = YES;
    } else {
        self.navigationItem.rightBarButtonItem.title = @"编辑";
        NSLog(@"muldata =========== %@",_dataMutableArray);
        _tableView.editing = NO;
    }
    
}

#pragma mark - data

- (void)getDataSource {
    [self dataMutableArray];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * const TableViewCellIdentifier = @"TableViewCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];
        
    }
     // 可以单独运行一下看一下cell的contentView的位置,当编辑时可以看出contentView整体向右移动了
   // cell.contentView.backgroundColor = [UIColor greenColor];
   // cell.backgroundColor = [UIColor yellowColor];
      cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row + 1];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.editing == YES) {
        [_dataMutableArray addObject:[NSNumber numberWithInt:(int)indexPath.row]];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.editing == YES) {
        [_dataMutableArray removeObject:[NSNumber numberWithInt:(int)indexPath.row]];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete & UITableViewCellEditingStyleInsert;
}

@end

参考链接

二: 实现自定义的cell多选

  • 效果如下图:
屏幕快照 2016-11-07 上午10.51.18.png
  • VC代码
#import "ViewController.h"
#import "LXKMultiSelectTableViewCell.h"

static NSString * const LXKMultiSelectTableViewCellIdentifier = @"LXKMultiSelectTableViewCellIdentifier";

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataMutableArray; // 数据源

@end

@implementation ViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self getDataSource];
    [self addSubViewSetSize];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(tableViewEdit)];
}

#pragma mark - Custom Accessors;

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.allowsMultipleSelection = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}

- (NSMutableArray *)dataMutableArray {
    if (!_dataMutableArray) {
        _dataMutableArray = [NSMutableArray array];
    }
    return _dataMutableArray;
}

#pragma mark - private

- (void)addSubViewSetSize {
    
    [self.view addSubview:self.tableView];
    [_tableView registerClass:[LXKMultiSelectTableViewCell class] forCellReuseIdentifier:LXKMultiSelectTableViewCellIdentifier];
}

#pragma mark - action

- (void)tableViewEdit {
    
    
    if (!_tableView.editing) {
     //   [_dataMutableArray removeAllObjects]; // 看是否需要第二次保留上一次的选中
    //    [_tableView reloadData]; 
        self.navigationItem.rightBarButtonItem.title = @"完成"; 
        _tableView.editing = YES;
    } else {
        self.navigationItem.rightBarButtonItem.title = @"编辑";
        NSLog(@"muldata =========== %@",_dataMutableArray);
        _tableView.editing = NO;
    }
    
}

#pragma mark - data

- (void)getDataSource {
    [self dataMutableArray];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LXKMultiSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LXKMultiSelectTableViewCellIdentifier];
    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    cell.row = indexPath.row;
    __weak typeof(self) weakSelf = self;
    cell.customSelectedBlock = ^ (BOOL selected, NSInteger row)
    {
        if (selected) {
            [weakSelf.dataMutableArray addObject:@(row)];
        }else
        {
            [weakSelf.dataMutableArray removeObject:@(row)];
        }
    };
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(LXKMultiSelectTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.dataMutableArray containsObject:@(cell.row)]) {
        [cell.btnSelect setTitle:@"🔴" forState:UIControlStateNormal];
    }else
    {
        [cell.btnSelect setTitle:@"⭕️" forState:UIControlStateNormal];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete & UITableViewCellEditingStyleInsert;
}

@end
  • Cell.h代码

#import <UIKit/UIKit.h>

typedef void(^CustomSelectBlock)(BOOL selected, NSInteger row);

@interface LXKMultiSelectTableViewCell : UITableViewCell

/**单元格复用 所以需要从VC中传递过来*/
@property (nonatomic, assign) NSInteger row;

/**选中的按钮*/
@property (nonatomic, strong) UIButton *btnSelect;

/**是否选中 多个单元格需要从VC中插看是否被选中*/
@property (nonatomic, getter=isCustomSelected) BOOL customSelected;

/**传递到VC中的点击事件回调*/
@property (nonatomic, copy) CustomSelectBlock customSelectedBlock;

@end
  • cell.m代码
#import "LXKMultiSelectTableViewCell.h"

@implementation LXKMultiSelectTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UIView *backgroundView = [[UIView alloc] initWithFrame:self.contentView.bounds];
        backgroundView.backgroundColor = [UIColor clearColor];
        self.backgroundView = backgroundView;
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        [backgroundView addSubview:self.btnSelect];
        _btnSelect.frame = CGRectMake( 0, 3, 38, 38);
        self.contentView.backgroundColor = [UIColor yellowColor];
        [_btnSelect addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(UIButton *)btnSelect {
    if (!_btnSelect) {
        _btnSelect = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnSelect setTitle:@"⭕️" forState:UIControlStateNormal];
        _btnSelect.titleLabel.font = [UIFont systemFontOfSize:20];
        _btnSelect.backgroundColor = [UIColor greenColor];
    }
    return _btnSelect;
}

//直接调用setLayoutSubviews。
//addSubview的时候。
//当view的frame发生改变的时候。
//滑动UIScrollView的时候。
//旋转Screen会触发父UIView上的layoutSubviews事件。
//改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。
- (void)layoutSubviews {
    [super layoutSubviews];
     _btnSelect.frame = CGRectMake( 0, 3, 38, 38);
     _btnSelect.imageView.contentMode = UIViewContentModeCenter;
}

- (void)selectAction:(UIButton *)sender{
    
    
    _customSelected = !_customSelected;
    if ([_btnSelect.titleLabel.text isEqualToString:@"⭕️"]) {
        [_btnSelect setTitle:@"🔴" forState:UIControlStateNormal];
    }else{
        [_btnSelect setTitle:@"⭕️" forState:UIControlStateNormal];
    }
    !_customSelectedBlock ?: _customSelectedBlock(_customSelected, _row);
}

@end

参考链接

三:联系人排序

  • 效果如下图:
屏幕快照 2016-11-08 上午9.52.23.png
@interface ContactModel : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic,strong) NSString *pinyin;//拼音

@end
  • ContactModel.m
#import "ContactModel.h"

@implementation ContactModel

- (void)setName:(NSString *)name{
    if (name) {
        _name = name;
        _pinyin = [self chineseTransformationPinyin:_name];
    }
}

// 中文转化成为拼音
- (NSString *)chineseTransformationPinyin:(NSString *)chinese {
    // 转换需要一个可变的字符串
    NSMutableString *str = [chinese mutableCopy];
    // 1.string: 需要转换的字符串。由于这个参数是 CFMutableStringRef 类型,一个 NSMutableString 类型也可以通过自由桥接的方式传入
    // 2.range: 转换操作作用的范围。这个参数是 CFRange,而不是 NSRange。当给定为NULL时,操作范围为全部
    // 3.transform: 需要应用的变换。这个参数使用了包含下面将提到的字符串常量的 ICU transform string
    // 4.reverse: 如有需要,是否返回反转过的变换。
    //    CFStringTransform(<#CFMutableStringRef string#>, <#CFRange *range#>, <#CFStringRef transform#>, <#Boolean reverse#>)
    //将汉字转换为拼音(带音标)
    CFStringTransform(( CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    // 不带声调的拼音
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
    
    //去掉空格
    return [str stringByReplacingOccurrencesOfString:@" " withString:@""];
}

@end
  • 格式化联系人的,获取一个二好友列表的一个二维数组,一是section数,二是cell的模型
  • LXKContactDataHelper.h
#import <Foundation/Foundation.h>

/**
 *  格式化联系人列表
 */

@interface LXKContactDataHelper : NSObject

+ (NSMutableArray *) getFriendListDataBy:(NSMutableArray *)array;

+ (NSMutableArray *) getFriendListSectionBy:(NSMutableArray *)array;

@end

  • LXKContactDataHelper.m

#import "LXKContactDataHelper.h"
#import "ContactModel.h"
#import <UIKit/UIKit.h>

@implementation LXKContactDataHelper

+ (NSMutableArray *) getFriendListDataBy:(NSMutableArray *)array{
    NSMutableArray *ans = [[NSMutableArray alloc] init];
    
    NSArray *serializeArray = [(NSArray *)array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {//排序
        int i;
        NSString *strA = ((ContactModel *)obj1).pinyin;
        NSString *strB = ((ContactModel *)obj2).pinyin;
        for (i = 0; i < strA.length && i < strB.length; i ++) {
            char a = [strA characterAtIndex:i];
            char b = [strB characterAtIndex:i];
            if (a > b) {
                return (NSComparisonResult)NSOrderedDescending;//上升
            }
            else if (a < b) {
                return (NSComparisonResult)NSOrderedAscending;//下降
            }
        }
        
        if (strA.length > strB.length) {
            return (NSComparisonResult)NSOrderedDescending;
        }else if (strA.length < strB.length){
            return (NSComparisonResult)NSOrderedAscending;
        }else{
            return (NSComparisonResult)NSOrderedSame;
        }
    }];
    
    char lastC = '1';
    NSMutableArray *data;
    NSMutableArray *oth = [[NSMutableArray alloc] init];
    for (ContactModel *user in serializeArray) {
        char c = [user.pinyin characterAtIndex:0];
        if (!isalpha(c)) {
            [oth addObject:user];
        }
        else if (c != lastC){
            lastC = c;
            if (data && data.count > 0) {
                [ans addObject:data];
            }
            
            data = [[NSMutableArray alloc] init];
            [data addObject:user];
        }
        else {
            [data addObject:user];
        }
    }
    if (data && data.count > 0) {
        [ans addObject:data];
    }
    if (oth.count > 0) {
        [ans addObject:oth];
    }
    return ans;
}

+ (NSMutableArray *)getFriendListSectionBy:(NSMutableArray *)array{
    NSMutableArray *section = [[NSMutableArray alloc] init];
    [section addObject:UITableViewIndexSearch];
    for (NSArray *item in array) {
        ContactModel *user = [item objectAtIndex:0];
        char c = [user.pinyin characterAtIndex:0];
        if (!isalpha(c)) {
            c = '#';
        }
        [section addObject:[NSString stringWithFormat:@"%c", toupper(c)]];
    }
    return section;
}

@end
  • VC里面的使用:
#import "ViewController.h"
#import "ContactModel.h"
#import "LXKContactDataHelper.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contactListRowMutableArray; // 一个二维数组 一是section的数量  二是每一个section地下row的行数
@property (nonatomic, strong) NSMutableArray *contactListSectionMutableArray; // 每个section头部的的数据

@end

@implementation ViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"联系人";
    [self getDataSource];
    [self addSubViewSetSize];
}

#pragma mark - Custom Accessors

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewScrollPositionNone;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableArray *)contactListRowMutableArray {
    if (!_contactListRowMutableArray) {
        _contactListRowMutableArray = [NSMutableArray array];
    }
    return _contactListRowMutableArray;
}

- (NSMutableArray *)contactListSectionMutableArray {
    if (!_contactListSectionMutableArray) {
        _contactListSectionMutableArray = [NSMutableArray array];
    }
    return _contactListSectionMutableArray;
}

#pragma mark - private

- (void)addSubViewSetSize {
    
    [self.view addSubview:self.tableView];
}

#pragma mark - data

- (void)getDataSource {
    NSArray *contactData = @[@"张三",@"李四",@"王麻子",@"2er",@"asdfhjk",@"王山",@"网",@"1"];
    NSMutableArray *tempMultableArray = [NSMutableArray array];
    for (NSString *tempString in contactData) {
        ContactModel *model = [[ContactModel alloc] init];
        model.name = tempString;
        [tempMultableArray addObject:model];
    }
    self.contactListRowMutableArray = [LXKContactDataHelper getFriendListDataBy:tempMultableArray];
    self.contactListSectionMutableArray = [LXKContactDataHelper getFriendListSectionBy:[_contactListRowMutableArray mutableCopy]];
    
    NSLog(@"row === %@",_contactListRowMutableArray);
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _contactListRowMutableArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_contactListRowMutableArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * const TableViewCellIdentifier = @"TableViewCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];
    }
    ContactModel *model = _contactListRowMutableArray[indexPath.section][indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}

// 头部滑动时候的标题
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index - 1;
}

#pragma mark - UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //viewforHeader
    id label = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    if (!label) {
        label = [[UILabel alloc] init];
        [label setFont:[UIFont systemFontOfSize:14.5f]];
        [label setTextColor:[UIColor grayColor]];
        [label setBackgroundColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1]];
    }
    [label setText:[NSString stringWithFormat:@"  %@",_contactListSectionMutableArray[section+1]]];
    return label;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 22.0;
}

@end

demo地址

  • 其实都是为了实现如下图的效果,系统的不行,不知道为啥:


    屏幕快照 2016-11-08 上午10.55.38.png

相关文章

网友评论

      本文标题:UITabelView多选和联系人排序

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