UITableViewController

//
//  ViewController.m
//  TableDemo
//
//  Created by djy dda on 12-7-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import <Foundation/Foundation.h>

@implementation ViewController

- (id)init{
    self = [super init];
    if(self !=nil){
        ///构建文件列表
        [self reload];
        ///初始化导航栏按钮
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(startEditing)];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Reload" style:UIBarButtonItemStylePlain target:self action:@selector(reload)];
        
    }
    return self;
}

- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView{
  return [NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",
                                          @"G",@"H",@"I",@"J",@"K",@"L",
                                          @"M",@"N",@"O",@"P",@"Q",@"R",
                                          @"S",@"T",@"U",@"V",@"W",@"X",
                                          @"Y",@"Z",@"#", nil];  
}
- (void) startEditing{
    [self.tableView setEditing:YES animated:YES];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(stopEditing)];
                                            
}

-(void) stopEditing{
    [self.tableView setEditing:YES animated:YES];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(startEditing)];
}

-(void) reload {
    NSDirectoryEnumerator *dirEnum;
    NSString *file;
    for(int i =0; i<27; i++){
        fileList [i] = [[NSMutableArray alloc] init];
    }
    dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:NSHomeDirectory()];
    
    while ((file = [dirEnum nextObject])) {
        char index = ([file cStringUsingEncoding:NSASCIIStringEncoding ])[0];
        if(index >='a' && index <='z'){
            index -=32;
        }
        if (index >='A' && index <='Z') {
            index -=65;
            [fileList[(int)index] addObject:file];
        }else{
            [fileList[26] addObject:file];
        }
    }
    nActiveSections = 0;
    activeSections = [[NSMutableArray alloc] init];
    sectionTitles = [[NSMutableArray alloc] init];
    for (int i = 0; i< 27; i++) {
        if([fileList[i] count]>0){
            nActiveSections ++;
            [activeSections addObject:fileList[i]];
             if(i<26){
                 [sectionTitles addObject:[NSString stringWithFormat:@"%C",i+65]];
                  }else{
                      [sectionTitles addObject:@"0-9"];
                  }
        }
    }
    [self.tableView reloadData];
    
}

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

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

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

- (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    int i=0;
    for(NSString *sectionTitle in sectionTitles){
        if([sectionTitle isEqualToString:title]){
            [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            return i;
        }
        i++;
    }
    return -1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [[activeSections objectAtIndex:[indexPath indexAtPosition:0]] objectAtIndex:[indexPath indexAtPosition:1]];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        cell.textLabel.text = CellIdentifier;
      
    }
    return cell;
}

- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(editingStyle ==UITableViewCellEditingStyleDelete){
        ///从数据园中删除
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        for (int i=0; i<[[activeSections objectAtIndex:[indexPath indexAtPosition:0]] count]; i++) {
            
            if([cell.textLabel.text isEqualToString:[[activeSections objectAtIndex:[indexPath indexAtPosition:0]]
                                                     objectAtIndex:i]]){
            
                [[activeSections objectAtIndex:[indexPath indexAtPosition:0]] removeObjectAtIndex:i];
            }
        }
        ///从表格中删除
        NSMutableArray *array = [[NSMutableArray alloc ]init];
        [array addObject: indexPath];
        [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
    }
    
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    UIAlertView *alert = [[UIAlertView alloc ] initWithTitle:@"File Selected" 
                                           message:[NSString stringWithFormat:@"you selected the file '%@'",cell.textLabel.text] 
                                           delegate:nil 
                                           cancelButtonTitle:nil 
                                           otherButtonTitles:@"OK", nil];
    [alert show];
    
}

- (void) loadView{
    [super loadView];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

-(void) didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}


@end

辛苦敲打的代码 留作纪念~~

posted on 2012-07-19 13:24  djy_fn  阅读(291)  评论(0编辑  收藏  举报

导航