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

       

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值