TableViewCell的点击打钩

祁晟
2023-12-01

//

//  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 {

    [super viewDidLoad];

    _classArray = [[NSMutableArray alloc]init];

    _indexArr = [[NSMutableArray alloc]init];

    _dataDic = [[NSMutableDictionary alloc]init];

    currentIndex = -1;

    currentsection = -1;

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

    _classTableview.delegate = self;

    _classTableview.dataSource = self;

    _classTableview.tableFooterView = [[UIView alloc]init];

    if ([_classTableview respondsToSelector:@selector(setSeparatorInset:)]) {

        [_classTableview setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];

    }

    if ([_classTableview respondsToSelector:@selector(setLayoutMargins:)]) {

        [_classTableview setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];

    }

    [self.view addSubview:_classTableview];

    [self getClassData];


   

    

}


//- (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) {

        [_classArray removeAllObjects];

        if (_classArray == nil) {

            _classArray = [NSMutableArray array];

        }

    }

    //

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

    

    

    for (NSString *className in classes) {

        [_classArray addObject:className];

    }

    _indexArr = [self getIndexArr:_classArray];

    for (NSString *indexStr in _indexArr) {

        NSMutableArray *sourceArray = [[NSMutableArray alloc]init];

        for (NSString *classStr in _classArray) {

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

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

            

            if ([indexStr isEqualToString:className]) {

                

                [sourceArray addObject:classStr];

            }

            

        }

        [_dataDic setObject:sourceArray forKey:indexStr];

        

    }

    [_classTableview reloadData];

    

    

}

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

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

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

    NSMutableArray *indexArray = [NSMutableArray array];

    //获取字母列表

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

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

        NSString *schoolName = [NSString stringWithFormat:@"%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 = [_indexArr objectAtIndex: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 *character in _indexArr)

    {

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

        count ++;

    }

    

    return 0;

}

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

    

    NSArray *tempArr = [NSArray array];

    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 = [NSString stringWithFormat:@"%@",_indexArr[indexPath.section]];

    NSArray *classArr = [_dataDic objectForKey:sectionStr];

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

    if (currentsection == indexPath.section) {

        if (indexPath.row == currentIndex) {

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

            accessoryView.image = [UIImage imageNamed:@"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;

    [_classTableview reloadData];


}


- (void)submitApplication:(id)sender{

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

    if (backView != nil) {

        [backView removeFromSuperview];

    }

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

    if (alertView != nil) {

        [alertView removeFromSuperview];

    }

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


 类似资料: