iOS開發學習之#簡單通訊錄的製作#

來源:互聯網
上載者:User

標籤:通訊錄   標籤   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

       

iOS開發學習之#簡單通訊錄的製作#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.