zoukankan      html  css  js  c++  java
  • ios开发 单词排序

    前阵子见到一个应用,是有那种单词排序的,还以为是用了什么 高深的技术,昨天在一本书上看到这个例子,只要用NSArray类内置的按字母顺序进行排序的功能就行了,使用sortedArrayUsingSelect()函数并将比较函数设置成系统的compare()函数即可。

    效果图如下:

    #import <UIKit/UIKit.h>

    @interface VocabularyAppDelegate : NSObject <UIApplicationDelegate>
    {
    UIWindow *window;
    }

    @property (nonatomic, retain) IBOutlet UIWindow *window;

    @end
    #import "VocabularyAppDelegate.h"
    #import "WordListTableViewController.h"

    @implementation VocabularyAppDelegate

    @synthesize window;

    #pragma mark -
    #pragma mark Application lifecycle

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    WordListTableViewController *wltvc=[[WordListTableViewController alloc]init];
    UINavigationController *nav=[[UINavigationController alloc]init];
    [nav pushViewController:wltvc animated:NO];
    [wltvc release];
    [window addSubview:nav.view];

    [self.window makeKeyAndVisible];
    return YES;
    }

    - (void)applicationWillResignActive:(UIApplication *)application
    {
    /*
    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    */
    }

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
    /*
    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    */
    }

    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
    /*
    Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    */
    }

    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
    /*
    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    */
    }

    - (void)applicationWillTerminate:(UIApplication *)application
    {
    /*
    Called when the application is about to terminate.
    Save data if appropriate.
    See also applicationDidEnterBackground:.
    */
    }

    - (void)dealloc
    {
    [window release];
    [super dealloc];
    }

    @end
    #import <Foundation/Foundation.h>

    @interface WordListTableViewController : UITableViewController
    {
    NSMutableDictionary *word;
    NSArray *sections;
    }

    @end
    #import "WordListTableViewController.h"
    #import "DefinitionViewController.h"

    @interface WordListTableViewController()
    @property(retain)NSMutableDictionary *words;
    @property(retain)NSArray *sections;
    @end

    @implementation WordListTableViewController
    @synthesize words,sections;

    -(NSMutableDictionary *)words
    {
    if(!words){
    NSBundle *bundle=[NSBundle mainBundle];
    NSString *filePath=[bundle pathForResource:@"words" ofType:@"txt"];

    words=[[NSDictionary dictionaryWithContentsOfFile:filePath]retain];
    }
    return words;
    }

    -(NSArray *)sections
    {
    if(!sections){
    sections=[[[self.words allKeys]sortedArrayUsingSelector:@selector(compare:)]retain];
    }
    return sections;
    }

    -(void)viewDidLoad
    {
    [super viewDidLoad];
    self.title=@"Vocabulary Sorting";
    self.navigationItem.rightBarButtonItem=self.editButtonItem;
    }

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
    return YES;
    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return self.sections.count;
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    NSArray *wordsInSection=[self.words objectForKey:[self.sections
    objectAtIndex:section]];
    return wordsInSection.count;
    }

    -(NSString *)wordAtIndexPath:(NSIndexPath *)indexPath
    {
    NSArray *wordsInSection=[self.words objectForKey:[self.sections
    objectAtIndex:indexPath.section]];
    return [wordsInSection objectAtIndex:indexPath.row];

    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier=@"WordListTableViewCell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
    cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
    reuseIdentifier:CellIdentifier]autorelease];
    }
    cell.textLabel.text=[self wordAtIndexPath:indexPath];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    return cell;
    }

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if(editingStyle==UITableViewCellEditingStyleDelete){
    NSString *section=[self.sections objectAtIndex:indexPath.section];
    NSMutableArray *wordsInSection=[[self.words objectForKey:section]mutableCopy];
    [wordsInSection removeObjectAtIndex:indexPath.row];
    [self.words setObject:wordsInSection forKey:section];
    [wordsInSection release];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if(editingStyle==UITableViewCellEditingStyleInsert){

    }
    }

    -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    {
    return [self.sections objectAtIndex:section];
    }

    -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
    return self.sections;
    }

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    DefinitionViewController *dvc=[[DefinitionViewController alloc]init];
    dvc.word=[self wordAtIndexPath:indexPath];
    [self.navigationController pushViewController:dvc animated:YES];
    [dvc release];
    }


    -(void)dealloc
    {
    [words release];
    [sections release];
    [super dealloc];
    }





    @end
    #import <Foundation/Foundation.h>

    @interface DefinitionViewController : UIViewController
    {
    UIWebView *webView;
    NSString *word;
    }

    @property(copy)NSString *word;

    @end
    #import "DefinitionViewController.h"

    @implementation DefinitionViewController

    @synthesize word;

    -(NSURLRequest *)urlRequest
    {
    NSString *urlString=@"http://www.google.com/dictionary";

    if (self.word) urlString = [urlString stringByAppendingFormat:@"?langpair=en%%7Cen&q=%@", self.word];
    return [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    }

    -(void)setWord:(NSString *)newWord
    {
    if(word!=newWord){
    [word release];
    word=[newWord copy];
    }
    self.title=word;
    if(webView.window)
    [webView loadRequest:[self urlRequest]];
    }

    -(void)loadView
    {
    webView=[[UIWebView alloc]initWithFrame:[[UIScreen mainScreen]
    applicationFrame]];
    webView.scalesPageToFit=YES;
    self.view=webView;
    }

    -(void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];;
    [webView loadRequest:[self urlRequest]];
    }

    -(void)dealloc
    {
    [webView release];
    [word release];
    [super dealloc];
    }

    @end








  • 相关阅读:
    迭代加深搜索 codevs 2541 幂运算
    二叉树结构 codevs 1029 遍历问题
    深搜+DP剪枝 codevs 1047 邮票面值设计
    2016.6.10 深度优先搜索练习
    二分+动态规划 POJ 1973 Software Company
    tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
    tarjan算法+缩点:求强连通分量 POJ 2186
    tarjan算法求割点cojs 8
    关键路径 SDUTOJ 2498
    第二章 STM32的结构和组成
  • 原文地址:https://www.cnblogs.com/hxxy2003/p/2223706.html
Copyright © 2011-2022 走看看