码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发学习之#简单通讯录的制作#

时间:2014-12-03 12:32:10      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:通讯录   标签   ios   导航控制器   标签栏控制器   

(1)创建一个项目telephoneBook

(2)打开Main.storyboard文件,从视图库图拖一个Tab bar Controller标签栏控制器到画布中。

(3)在工具栏中,选择show the Attributes inspector图标,在View controller下,选择Is Initial View Controller。

(4)将画布中的原有的view controller视图控制器删掉,再将Tab Bar controller标签栏控制器关联view controller-Item 1和view controller-Item2视图控制器的视图删除。

(5)从视图库中拖一个Navigation controller导航控制器到画布中。

(6)将Tab Bar Controller标签栏控制器关联视图变为Navigation Controller导航控制器。

核心代码:

TableView1.h

#import <UIKit/UIKit.h>

@interface TableView1 : UITableViewController<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray *a;
}

- (IBAction)aa:(id)sender;

- (IBAction)bb:(id)sender;
@end

TableView1.m

#import "TableView1.h"

@interface TableView1 ()

@end

@implementation TableView1

- (void)viewDidLoad {
    a = [[NSMutableArray alloc]initWithObjects:@"ant",@"alpaca",@"albatross",@"badger",@"bat",@"bear",@"cat",@"calf",@"cattle", nil];
    
    [super viewDidLoad];
    
}

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

#pragma mark - Table view data source

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

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

    return [a count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [a objectAtIndex:[indexPath row]];
    return cell;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [a removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (IBAction)aa:(id)sender {
    [self setEditing:YES];
}

- (IBAction)bb:(id)sender {
    [self setEditing:NO];
}
@end

TableView2.h

#import <UIKit/UIKit.h>

@interface TableView2 : UITableViewController<UITableViewDataSource,UITableViewDelegate>{
    NSDictionary *list;
    NSArray *ff;
    IBOutlet UISearchBar *searchBar;
    BOOL isSearchOn;
    BOOL canSelectRow;
    NSMutableArray *listOfMovies;
    NSMutableArray *searchResult;
}

- (void)searchMoviesTableView;

@end


TableView2.m

#import "TableView2.h"

@interface TableView2 ()

@end

@implementation TableView2

- (void)viewDidLoad {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"plist"];
    NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
    list = dic;
    NSArray *array = [[list allKeys]sortedArrayUsingSelector:@selector(compare:)];
    ff = array;
    self.tableView.tableHeaderView = searchBar;
    searchBar.autocorrectionType = UITextAutocorrectionTypeYes;
    listOfMovies = [[NSMutableArray alloc]init];
    for (NSString *year in array) {
        NSArray *movies = [list objectForKey:year];
        for (NSString *title in movies) {
            [listOfMovies addObject:title];
        }
    }
    searchResult = [[NSMutableArray alloc]init];
    isSearchOn = NO;
    canSelectRow = YES;
    
    [super viewDidLoad];
    
}

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

#pragma mark - Table view data source

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (canSelectRow) {
        return indexPath;
    }else{
        return nil;
    }
}

- (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString *)searchText{
    if ([searchText length] > 0) {
        isSearchOn = YES;
        canSelectRow = YES;
        self.tableView.scrollEnabled = YES;
        [self searchMoviesTableView];
    }else{
        isSearchOn = NO;
        canSelectRow = NO;
        self.tableView.scrollEnabled = NO;
    }
    [self.tableView reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar{
    [self searchMoviesTableView];
}

- (void) searchMoviesTableView{
    [searchResult removeAllObjects];
    for (NSString *str in listOfMovies) {
        NSRange titleResultRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
        if (titleResultRange.length > 0) {
            [searchResult addObject:str];
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (isSearchOn) {
        return 1;
    }else{
        return [ff count];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (isSearchOn) {
        return [searchResult count];
    }else{
        NSString *year = [ff objectAtIndex:section];
        NSArray *movieSection = [list objectForKey:year];
        return [movieSection count];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (isSearchOn) {
        NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }else{
        NSString *year = [ff objectAtIndex:[indexPath section]];
        NSArray *movieSection = [list objectForKey:year];
        cell.textLabel.text = [movieSection objectAtIndex:[indexPath row]];
    }
    
    return cell;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *year = [ff objectAtIndex:section];
    if (isSearchOn) {
        [searchBar resignFirstResponder];
        return nil;
    }else{
        return year;
    }
}


@end

bubuko.com,布布扣bubuko.com,布布扣       bubuko.com,布布扣bubuko.com,布布扣

iOS开发学习之#简单通讯录的制作#

标签:通讯录   标签   ios   导航控制器   标签栏控制器   

原文地址:http://blog.csdn.net/u010710758/article/details/41693829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!