TableViewCell的点击打钩

来源:互联网 发布:零基础学qt4编程 编辑:程序博客网 时间:2024/04/29 23:59

//

//  ViewController.m

//  TableViewCell的点击打钩

//

//  Created by siqiyang on 16/4/15.

//  Copyright © 2016 mengxianjin. All rights reserved.

//


#import "ViewController.h"

#import "ChineseToPinyin.h"

#define SCREEN_WIDTH ([[UIScreen mainScreen]bounds].size.width)

#define SCREEN_HEIGHT ([[UIScreen mainScreen]bounds].size.height)

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>


{

    NSInteger currentIndex;

    NSInteger currentsection;

}

@property (nonatomic,strong)NSMutableArray *classArray;

@property (nonatomic,strong)UITableView *classTableview;

@property (nonatomic,strong)NSMutableArray *indexArr;

@property (nonatomic,strong)NSMutableDictionary *dataDic;



@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    _classArray = [[NSMutableArrayalloc]init];

    _indexArr = [[NSMutableArrayalloc]init];

    _dataDic = [[NSMutableDictionaryalloc]init];

    currentIndex = -1;

    currentsection = -1;

    _classTableview = [[UITableViewalloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,SCREEN_HEIGHT)style:UITableViewStylePlain];

    _classTableview.delegate =self;

    _classTableview.dataSource =self;

    _classTableview.tableFooterView = [[UIViewalloc]init];

    if ([_classTableviewrespondsToSelector:@selector(setSeparatorInset:)]) {

        [_classTableviewsetSeparatorInset:UIEdgeInsetsMake(0,0, 0,0)];

    }

    if ([_classTableviewrespondsToSelector:@selector(setLayoutMargins:)]) {

        [_classTableviewsetLayoutMargins:UIEdgeInsetsMake(0,0, 0,0)];

    }

    [self.viewaddSubview:_classTableview];

    [selfgetClassData];


   

    

}


//- (void)addSelectedSchoolLabel{

//    UILabel *selectedLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];

//    selectedLabel.backgroundColor = [UIColor colorWithHexString:@"#363636"];

//    selectedLabel.textColor = [UIColor whiteColor];

//    selectedLabel.text = [NSString stringWithFormat:@"%@:%@",@"已选择学校",self.schoolName];

//    [self.view addSubview:selectedLabel];


//}

/**

 *  获取班级信息

 */

- (void)getClassData{

    

    if (_classArray.count !=0) {

        [_classArrayremoveAllObjects];

        if (_classArray ==nil) {

            _classArray = [NSMutableArrayarray];

        }

    }

    //

    NSArray *classes =@[@"三班",@"五班",@"无敌班",@"春天班",@"秋香班",@"唐寅班",@"夏荷班",@"冬梅班",@"香山班",@"北京班",@"上海班",@"广州班",@"杭州班",@"福州班",@"兰州班",@"深圳班",@"西安班",@"青岛班",@"哈尔滨班",@"沈阳班",@"大连班",@"南京班",@"济南班",@"郑州班",@"石家庄班",@"唐山班",@"张家口班",@"呼和浩特班",@"包头市班",@"长春市班",@"宁波市班",@"三明市班",@"济宁市班",@"日照班",@"vvgf",@"wrwr",@"#$rwfs",@"@#324$%#",@"2@23#$!"];

    

    

    for (NSString *classNamein classes) {

        [_classArray addObject:className];

    }

    _indexArr = [selfgetIndexArr:_classArray];

    for (NSString *indexStrin _indexArr) {

        NSMutableArray *sourceArray = [[NSMutableArrayalloc]init];

        for (NSString *classStrin _classArray) {

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

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

            

            if ([indexStr isEqualToString:className]) {

                

                [sourceArray addObject:classStr];

            }

            

        }

        [_dataDic setObject:sourceArray forKey:indexStr];

        

    }

    [_classTableviewreloadData];

    

    

}

#pragma mark -- 获取数组中的首字母合集

//获取数组中的首字母合集

- (NSMutableArray *)getIndexArr:(NSArray *)arr{

    NSMutableArray *indexArray = [NSMutableArrayarray];

    //获取字母列表

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

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

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

        //不添加重复元素

        if (![indexArray containsObject:[schoolName uppercaseString]]) {

            [indexArray addObject:[schoolName uppercaseString]];

        }

        

    }

    [indexArray sortUsingSelector:@selector(compare:)];

    return indexArray;

}

#pragma mark -- tableViewdatasource

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

    

    if (tableView == _classTableview) {

        return _indexArr;

    }

    return nil;

}

- (NSString *)tableView:(UITableView *)tableView

titleForHeaderInSection:(NSInteger)section {

    

    NSInteger tempSection = section;

    NSString *key = [_indexArrobjectAtIndex:tempSection];

    return key;

    

    

}


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

    

    return_indexArr.count;

    

}

- (NSInteger)tableView:(UITableView *)tableView

sectionForSectionIndexTitle:(NSString *)title

               atIndex:(NSInteger)index {

    

    NSInteger count = 0;

    

    for(NSString *characterin _indexArr)

    {

        if([character isEqualToString:title]) return count;

        count ++;

    }

    

    return 0;

}

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

    

    NSArray *tempArr = [NSArrayarray];

    if (_dataDic &&_indexArr) {

        tempArr = [_dataDic objectForKey:[_indexArr objectAtIndex:section]];

    }

    if (tempArr.count !=0) {

        return tempArr.count;

    }

    else

        return 0;

}


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

    

    static NSString *cellId =@"class";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

        cell.selectionStyle =UITableViewCellSelectionStyleNone;

    }

    //获取section上的串

    NSString *sectionStr = [NSStringstringWithFormat:@"%@",_indexArr[indexPath.section]];

    NSArray *classArr = [_dataDicobjectForKey:sectionStr];

    cell.textLabel.text =classArr[indexPath.row];

    if (currentsection == indexPath.section) {

        if (indexPath.row ==currentIndex) {

            UIImageView *accessoryView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 19, 16)];

            accessoryView.image = [UIImageimageNamed:@"icon_check_sel"];

            cell.accessoryView = accessoryView;

        }

        else

            cell.accessoryView = nil;

    }

    else

        cell.accessoryView = nil;

    

    return cell;

    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    return 50;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    

    currentIndex = indexPath.row;

    currentsection = indexPath.section;

    [_classTableviewreloadData];


}


- (void)submitApplication:(id)sender{

    UIView *backView = (UIView *)[self.viewviewWithTag:555];

    if (backView != nil) {

        [backView removeFromSuperview];

    }

    UIView *alertView = (UIView *)[self.viewviewWithTag:600];

    if (alertView != nil) {

        [alertView removeFromSuperview];

    }

    [self.navigationControllerpopToViewController:[self.navigationController.viewControllersobjectAtIndex:2] animated:YES];

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


1 0
原创粉丝点击