1. 程式人生 > >iOS 多選cell複用修正應該標記model裡是否選中

iOS 多選cell複用修正應該標記model裡是否選中

//

//  AddTeamMeberViewController.m

//  HengTaiXinGolf

//

//  Created by 歐陽榮 on 16/4/21.

//  Copyright © 2016 HengTaiXinGolf. All rights reserved.

//

#import "AddTeamMeberViewController.h"

#import "MemberBeanModel.h"

#import "AddTMCell.h"

#import "KCPinyinHelper.h"

#define ADDTECELL @"AddTMCell"

@interface

AddTeamMeberViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation AddTeamMeberViewController{

    NSMutableArray *_nameDataArr;

    NSMutableArray * _selecteArr;

}

@synthesize m_tableView;

@synthesize m_allName;

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];

if (self)

    {

        _nameDataArr = [NSMutableArray array];

        _dataSouceArray = [NSMutableArray array];

        _selecteArr = [NSMutableArray array];

        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(

0, 0 , 100, 44)];

        titleLabel.backgroundColor = [UIColor clearColor];  //設定Label背景透明

        titleLabel.font = [UIFont boldSystemFontOfSize:KTitle];  //設定文字字型與大小

        titleLabel.textColor = [UIColor whiteColor];  //設定文字顏色

        titleLabel.textAlignment = NSTextAlignmentCenter;

        titleLabel.text = @"選擇聯絡人"//設定標題

self.navigationItem.titleView = titleLabel;

    }

returnself;

}

//增加右鍵發表儲存按鈕

-(void)createRightBtn{

    _btn = [UIButton buttonWithType:UIButtonTypeCustom];

    _btn.frame = CGRectMake(0, 0, 40, 20);

    [_btn setTitle:@"確定" forState:UIControlStateNormal];

    [_btn addTarget:self action:@selector(rightBtnClicked) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:_btn];

self.navigationItem.rightBarButtonItem = item;

}

-(void)createLeftBtn{

    _btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

    _btn1.frame = CGRectMake(0, 0, 40, 20);

    [_btn1 setTitle:@"取消" forState:UIControlStateNormal];

    [_btn1 addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem * item1 = [[UIBarButtonItem alloc]initWithCustomView:_btn1];

self.navigationItem.leftBarButtonItem = item1;

}

- (void)viewDidLoad {

    [superviewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColorwhiteColor];

    [selfcreateLeftBtn];

    [selfcreateRightBtn];

    [selfcreateData];

    [selfgeturl:KgetAddTeamMemberList];

}

-(void)rightBtnClicked{

DSLog(@"確定");

    [selfsetTeamMemberAddUrl];

}

-(void)leftBtnClicked{

DSLog(@"取消");

    [self.navigationControllerpopViewControllerAnimated:YES];

}

-(void)setTeamMemberAddUrl

{

AFHTTPRequestOperationManager *man = [AFHTTPRequestOperationManagermanager];

    man.responseSerializer = [AFHTTPResponseSerializerserializer];

    man.requestSerializer = [AFJSONRequestSerializerserializer];//json格式來上傳引數

NSString *stringarr=[NSStringstring];

NSString *string;//[NSMutableString string];

for (int i=0; i<_selecteArr.count; i++) {

        string=[_selecteArrobjectAtIndex:i];

        stringarr=[stringarr stringByAppendingString:[NSStringstringWithFormat:@"%@,",string]];

    }

NSString *string2=[stringarr substringToIndex:stringarr.length-1];

NSString *mynoarray =[NSStringstringWithFormat:@"(%@)",string2];

DSLog(@"%@",mynoarray);

NSString * memNick = [UserModelsharedInstance].memberNick;

NSString *paraString = [MD5encodeString:memNick];

NSString * teaM = [MD5encodeString:self.teamName];

NSString *memStr = [UserModelsharedInstance].memberId;

//拒絕好友請求引數

NSMutableDictionary * _param = [NSMutableDictionarydictionary];

    [_param setValue:memStr forKey:@"memberId"];

    [_param setValue:paraString forKey:@"memberNick"];

    [_param setValue:self.teamNoforKey:@"teamNo"];

    [_param setValue:teaM forKey:@"teamName"];

    [_param setValue:mynoarray forKey:@"memberIdArray"];

    [_param setValue:KAPPIDforKey:@"appId"];

    [_param setValue:KappSecretforKey:@"appSecret"];

    [_param setValue:[CommonToolsgetTimeScamp] forKey:@"timestamp"];

NSArray * paramArr = @[[NSStringstringWithFormat:@"memberId%@",_param[@"memberId"]],[NSStringstringWithFormat:@"memberNick%@",_param[@"memberNick"]],[NSStringstringWithFormat:@"teamNo%@",_param[@"teamNo"]],[NSStringstringWithFormat:@"teamName%@",_param[@"teamName"]],[NSStringstringWithFormat:@"memberIdArray%@",_param[@"memberIdArray"]],[NSStringstringWithFormat:@"appId%@",_param[@"appId"]],[NSStringstringWithFormat:@"appSecret%@",_param[@"appSecret"]],[NSStringstringWithFormat:@"timestamp%@",_param[@"timestamp"]]];

    [_param setValue:[CommonToolstokenmd5StringFromArray:paramArr] forKey:@"digest"];

    [man POST:KsetTeamMemberAddparameters:_param success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSDictionary *dict = [NSJSONSerializationJSONObjectWithData:responseObject options:NSJSONReadingMutableContainerserror:nil];

NSLog(@"  邀請好友dict %@",dict);

if ([dict[@"returnCode"] isEqualToString:@"000000"]) {

            [MBProgressHUDshowSuccess:@"邀請成功"toView:nil];

            [self.navigationControllerpopViewControllerAnimated:YES];

        } else

        {

            [MBProgressHUDshowSuccess:@"邀請失敗"toView:nil];

        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"error %@",error);

    }];

}

-(void)geturl:(NSString *)url

{

NSString *memStr = [UserModelsharedInstance].memberId;

//判斷是否好友請求引數  getAddTeamMemberList

NSMutableDictionary * _param = [NSMutableDictionarydictionary];

    [_param setValue:memStr forKey:@"memberId"];

    [_param setValue:KAPPIDforKey:@"appId"];

    [_param setValue:KappSecretforKey:@"appSecret"];

    [_param setValue:[CommonToolsgetTimeScamp] forKey:@"timestamp"];

NSArray * paramArr = @[[NSStringstringWithFormat:@"memberId%@",_param[@"memberId"]],[NSStringstringWithFormat:@"appId%@",_param[@"appId"]],[NSStringstringWithFormat:@"appSecret%@",_param[@"appSecret"]],[NSStringstringWithFormat:@"timestamp%@",_param[@"timestamp"]]];

    [_param setValue:[CommonToolstokenmd5StringFromArray:paramArr] forKey:@"digest"];

    [HTTPRequestToolsendGetAFRequest:url withParameters:_param withSuccess:^(id message) {

NSDictionary *tempArr = [NSJSONSerializationJSONObjectWithData:message options:NSJSONReadingMutableContainerserror:nil];

/**

         *  請求資料  (判斷資料請求的型別)

         */

DSLog(@"      ----獲取新增球隊成員列表介面--     %@",tempArr);

if (_nameDataArr||_dataSouceArray) {

            [_nameDataArrremoveAllObjects];

            [_dataSouceArrayremoveAllObjects];

        }

for (NSDictionary *tempDic in tempArr[@"teamMemberList"])

        {

MemberBeanModel *contactModel  = [[MemberBeanModelalloc]init];

            [contactModel setValuesForKeysWithDictionary:tempDic];

            [_dataSouceArrayaddObject:contactModel];//所有好友資訊陣列

            [_nameDataArraddObject:contactModel.memberNick];//所有好友暱稱陣列

        }

        [selfreloadDataUI];

    } andWithFail:^(id message) {

DSLog(@"error = %@",message);

    }];

}

-(void)reloadDataUI{

DSLog(@"======  所有人的名字    _nameDataArr======%@",_nameDataArr);

self.m_allName = [[NSMutableArrayalloc]init];

for (int i = 0; i < _nameDataArr.count; i ++) {

char firstChar = pinyinFirstLetter([[_nameDataArrobjectAtIndex:i]characterAtIndex:0]);

NSString *youName = [NSStringstringWithFormat:@"%c",firstChar];

//不新增重複元素

if (![m_allNamecontainsObject:[youName uppercaseString]]) {

            [self.m_allNameaddObject:[youName uppercaseString]];

        }

    }

DSLog(@"======= 所有人名字的首字母大寫  =m_allName========%@",self.m_allName);//所有的索引大寫字母

    [self.m_allNamesortUsingSelector:@selector(compare:)];//給所有索引大寫字母排序

nameDic = [[NSMutableDictionaryalloc]init];//每個大寫字母對應的名字字典

//每個section對應的行數列表

for (NSString *sectionString inm_allName) {

NSMutableArray *rowSource = [[NSMutableArrayalloc]init];

for (NSString *charString in_nameDataArr) {

char firstChar = pinyinFirstLetter([charString characterAtIndex:0]);

NSString *youName = [NSStringstringWithFormat:@"%c",firstChar];

if ([sectionString isEqualToString:[youName uppercaseString]]) {

                [rowSource addObject:charString];

            }

        }

        [nameDicsetValue:rowSource forKey:sectionString];

    }

DSLog(@"======nameDic======%@",nameDic);//每個大寫字母對應的名字字典

    [self.m_tableViewreloadData];

}

-(void)createData{

self.m_tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT-64)style:UITableViewStylePlain];

    [self.m_tableViewregisterClass:[AddTMCellclass] forCellReuseIdentifier:ADDTECELL];

self.m_tableView.delegate = self;

self.m_tableView.dataSource = self;

self.m_tableView.tableFooterView = [[UIViewalloc] initWithFrame:CGRectZero];

    [self.viewaddSubview:self.m_tableView];

}

#pragma mark - UITableView delegate

//m_allName 大寫字母   nameDic  所有字母對應的名字

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSArray *dicCount = [nameDicobjectForKey:[m_allNameobjectAtIndex:section]];

return [dicCount count];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return [m_allNamecount];

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return [m_allNameobjectAtIndex:section];

}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

//新增通訊錄索引

NSMutableArray *toBeRetured = [[NSMutableArrayalloc]init];

for (char c = 'A'; c <= 'Z'; c ++) {

        [toBeRetured addObject:[NSStringstringWithFormat:@"%c",c]];

    }

return toBeRetured;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return36 * KSCALE_X;

}

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

{

AddTMCell *cell = [tableView dequeueReusableCellWithIdentifier:ADDTECELL];

if (cell == nil) {

        cell = [[AddTMCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ADDTECELL];

    }

//    cell.selectionStyle = UITableViewCellSelectionStyleNone;

//每個字母對應的名字

NSString *sectionString = [m_allNameobjectAtIndex:indexPath.section];

NSArray *allShowName = [nameDicobjectForKey:sectionString];

if (allShowName.count > 0) {

        cell.nameLabel.text = [allShowName objectAtIndex:indexPath.row];

    }

//_dataSouceArray  所有人的資料模型   ---   在通過暱稱去找使用者的頭像

for (MemberBeanModel *contactModel in_dataSouceArray){

if ([contactModel.memberNickisEqualToString:[allShowName objectAtIndex:indexPath.row]]) {

CGSize nameSize = [contactModel.memberNicksizeWithAttributes:[NSDictionarydictionaryWithObject:[UIFontsystemFontOfSize:15 * KWIDTH] forKey:NSFontAttributeName]];

if (nameSize.width < 200 * KSCALE_X ) {

                cell.nameLabel.frame =CGRectMake(155 * KSCALE_X, 31 * KSCALE_X, nameSize.width, 31 * KSCALE_X);

            }

if ([contactModel.memberTeamlength]) {

                cell.selectImgView.image = [UIImageimageNamed:@"hyyj"];

                cell.teamNameLab.text = [NSStringstringWithFormat:@"%@",contactModel.memberTeam];

                cell.selectImgView.tag = 0;

            }else{

                cell.selectImgView.tag = 1 ;

                cell.selectImgView.image = [UIImageimageNamed:@"hywx"];

                cell.teamNameLab.text = @"";

            }