iOS 大学列表检索

#import "SchoolSelectViewController.h"
#import "InfoViewController.h"
#import "AsyConnectModel.h"
#import "Collect.h"
@interface SchoolSelectViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *messageArr_;
    NSMutableArray *schoolIdArr_;//为了ididid
    NSMutableArray *schoolNameArr_;//学校名字
    NSMutableArray *sectionArr_;
}
@end

@implementation SchoolSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"选择学校";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    self.navigationController.navigationBar.translucent = NO;

    messageArr_ = [[NSMutableArray alloc] init];
    schoolIdArr_ = [[NSMutableArray alloc] init];
    schoolNameArr_ = [[NSMutableArray alloc] init];
    sectionArr_ = [[NSMutableArray alloc] init];
    //将back->返回
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                             initWithTitle:@"返回"
                                             style:UIBarButtonItemStylePlain
                                             target:self
                                             action:nil];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HIGHT - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.alpha = 0.5;
        self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HIGHT)];
    [image setImage:[UIImage imageNamed:@"hundan_gray"]];
    image.userInteractionEnabled = YES;
    [self.view addSubview:image];
    [self.view addSubview:self.tableView];

    [self schoolListConnect];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = NAVIGATION_COLOR;
    
}
#pragma mark - UITableViewDataSource

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return sectionArr_;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [sectionArr_ objectAtIndex:section];
    return key;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //分组数 也就是section数
    return sectionArr_.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[schoolNameArr_ objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    static NSString *identifier = @"SchoolSelectViewController";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }
    cell.textLabel.text = [[schoolNameArr_ objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
   
        return cell;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}
#pragma mark -
#pragma mark - UITableViewDelegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
    lab.backgroundColor = [UIColor grayColor];
    lab.text = [sectionArr_ objectAtIndex:section];
    lab.textColor = [UIColor darkGrayColor];
    return lab;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    InfoViewController *info = [[InfoViewController alloc] init];
    
    NSString *name = [[schoolNameArr_ objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    NSString *schoolId = [[schoolIdArr_ objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    NSLog(@"学校id====%@",schoolId);
    info.schoolName = name;
    info.clubName = self.clubName;
    info.clubID = self.clubIdStr;
    info.schoolID = schoolId;
    [self.navigationController pushViewController:info animated:YES];
}
//学校列表
- (void)schoolListConnect
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.clubIdStr,@"clubId", nil];
    NSString *jsonString = [AsyConnectModel DataTOjsonString:dic];
    NSDictionary *postDic = [AsyConnectModel setPostValue:jsonString];
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",ServerKey,AF_schooLlist];
    [AsyConnectModel asyncPostWithUrl:postUrl strparmaters:postDic finishBlock:^(id response){
//        NSLog(@"学校列表返回response== %@",response);
        if ([response isKindOfClass:[NSError class]]) {
            NSError *error = (NSError *)response;
            NSLog(@"%@",error.localizedDescription);
            UIView *view = WINDOW;
            [view makeToast:error.localizedDescription duration:TOAST_TIMER position:TOAST_CENTER];
            //            [HUD_ hide];
            return;
            
        }
        if ([[Collect replaceNull:[response objectForKey:@"result"]] integerValue] == 1) {
            [messageArr_ removeAllObjects];
            [messageArr_ addObjectsFromArray:[response objectForKey:@"message"]];
            [self handleDataFrom:messageArr_];
            [self section:messageArr_];
            [self schoolName:messageArr_];
            [self.tableView reloadData];

        } else {
            
        }
        
    }];
}
- (NSMutableArray *)handleDataFrom:(NSArray *)arr{
    NSString *tempString = @"";
    for (NSDictionary *dic in arr) {
        NSMutableArray *elementArr = [NSMutableArray array];
        NSString *firstLetter = [[dic objectForKey:@"firstLetter"] description];
        // 判断return的数组里是不是已经添加过当前遍历到的首字母
        if ([tempString isEqualToString:firstLetter]) {
            elementArr = [NSMutableArray arrayWithArray:[schoolIdArr_ lastObject]];
            [elementArr addObject:[[dic objectForKey:@"id"] description]];
            [schoolIdArr_ removeLastObject];
        } else {
            [elementArr addObject:[[dic objectForKey:@"id"] description]];
        }
        [schoolIdArr_ addObject:elementArr];
        tempString = firstLetter;
    }
    return schoolIdArr_;
}
- (NSMutableArray *)schoolName:(NSArray *)arr{
    NSString *tempString = @"";
    //    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    for (NSDictionary *dic in arr) {
        NSMutableArray *elementArr = [NSMutableArray array];
        NSString *firstLetter = [[dic objectForKey:@"firstLetter"] description];
        // 判断return的数组里是不是已经添加过当前遍历到的首字母
        if ([tempString isEqualToString:firstLetter]) {
            elementArr = [NSMutableArray arrayWithArray:[schoolNameArr_ lastObject]];
            [elementArr addObject:[[dic objectForKey:@"name"] description]];
            [schoolNameArr_ removeLastObject];
        } else {
            [elementArr addObject:[[dic objectForKey:@"name"] description]];
        }
        [schoolNameArr_ addObject:elementArr];
        tempString = firstLetter;
    }
    NSLog(@"schoolNameArr_====%@",schoolNameArr_);
    return schoolNameArr_;
}
- (NSMutableArray *)section:(NSArray *)arr{
    for (NSDictionary *dic in arr) {
        NSString *firstLetter = [[dic objectForKey:@"firstLetter"] description];
        if ([sectionArr_ containsObject:firstLetter]) {
            
        } else {
            [sectionArr_ addObject:firstLetter];
        }
    }return sectionArr_;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

猜你喜欢

转载自blog.csdn.net/YY_Seven/article/details/50588436
ios