• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            羅朝輝(飄飄白云)

            關注嵌入式操作系統,移動平臺,圖形開發。-->加微博 ^_^

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              85 隨筆 :: 0 文章 :: 169 評論 :: 0 Trackbacks
            [Cocoa]在工程中
            使用
            Three20 庫
            羅朝輝 (http://www.shnenglu.com/kesalin/
            CC 許可,轉載請注明出處
            Three20 是 facebook 開源的一款功能齊全又強大的庫,覆蓋 UI,network,JSON/XML解析等。其 github 倉庫在這里:https://github.com/facebook/three20 ,這個頁面也有如何在工程中添加 three20 庫的介紹,不過在 Lion 版下以及 xcode 4.2 下有些許不同,英文好的同學可以參看原文。現整理如下:
            1,新建一個名為 Three20Demo 的 Empty Application;

            2,在這頁面上下載 three20 zip源代碼工程;解壓到與 Three20Demo 項目平級的目錄下;

            3,拖拽 "three20/src/Three20/" 目錄下的 Three20.xcodeproj 到 Three20Demo 工程中,如下圖。


            4,選中  Three20Demo 的 target ,在 Build Phases 的 Link Binary With Libraries 中添加 three20 的靜態庫。如下圖:


            5,拖拽 "three20/src" 下面的Three20.bundle 到 Three20Demo 工程下,在彈出的對話框中不要選擇 Copy Item into 那個選項,選擇第二個 Create groups for any added folders。


            6,類似第4步,向 three20Demo 中添加 QuartzCore.framework 。

            7,在工程的 Build settings 中向 "Other Linker Flags" 添加 -ObjC 和 -all_load 兩項。


            8,編譯運行工程,然后至你自己用戶的 Library 目錄下拷貝 three20 頭文件至你的項目目錄下。(Lion版本無法查看隱藏目錄,command + shift + G ,然后輸入 ~/Library,就可以找到隱藏的library)。 three20 目錄位于:
            /Users/yourname/Library/Developer/Xcode/DerivedData/Three320Demo-XXXXXX/Build/Products/three20


            9,在工程的 Build settings 中向 "Header Search Paths" 添加 three20,并選中 Recursive 選項。


            10, 至此,所有的配置工作完成,你可以在工程中使用包含如下頭文件:#import <Three20/Three20.h> 來使用 Three20 庫。

            Three20 解壓的包里面有個 sample 目錄,里面展示了大部分 api 的使用,可以運行看看。
            下面我將演示如何使用 TTTableViewController。

            向工程中添加類 KSDataSource 這個類作為 TTTableView 的 datasource。
            KSDataSource.h
            //
            //  DataSource.h
            //  Three320Demo
            //
            //  Created by LuoZhaohui on 12/31/11.
            //  Copyright (c) 2011 kesalin@gmail.com. All rights reserved.
            //

            #import <Three20/Three20.h>

            // Model
            //
            @interface KSMockModel : NSObject <TTModel> 
            {
            @private
                NSArray * allNames;
                NSMutableArray * names;
                NSMutableArray * delegates;
                
                NSTimer* fakeSearchTimer;
                NSTimeInterval fakeSearchDuration;
                NSTimer* fakeLoadingTimer;
                NSTimeInterval fakeLoadingDuration;
            }

            @property(nonatomic,retain) NSArray * names;
            @property(nonatomic) NSTimeInterval fakeSearchDuration;
            @property(nonatomic) NSTimeInterval fakeLoadingDuration;

            + (NSMutableArray *) fakeNames;

            - (id)initWithNames:(NSArray *)names;
            - (void)search:(NSString *)text;

            @end

            // DataSource
            //
            @interface KSDataSource : TTSectionedDataSource
            {
                KSMockModel * mockModel;
            }

            @property(nonatomic,readonly) KSMockModel * mockModel;

            @end

            KSDateSource.m:
            //
            //  DataSource.m
            //  Three320Demo
            //
            //  Created by LuoZhaohui on 12/31/11.
            //  Copyright (c) 2011 kesalin@gmail.com. All rights reserved.
            //

            #import "KSDataSource.h"

            @interface KSMockModel ()

            - (void) loadNames;

            @end

            @implementation KSMockModel

            @synthesize names;
            @synthesize fakeSearchDuration, fakeLoadingDuration;

            + (NSMutableArray *) fakeNames
            {
                return [NSMutableArray arrayWithObjects:
                        @"Hector Lewis",
                        @"Juanita Fredrick",
                        @"Richard Raymond",
                        @"Marcia Myer",
                        @"Shannon Mahoney",
                        @"James Steiner",
                        @"Daniel Lloyd",
                        @"Fredrick Hutchins",
                        @"Tracey Smith",
                        @"Brandon Rutherford",
                        @"Megan Lopez",
                        @"Jean Trujillo",
                        @"Franklin Diamond",
                        @"Mildred Jacobsen",
                        @"Sandra Adams",
                        @"Debra Pugliese",
                        @"Cynthia Hall",
                        @"Joshua Hicks",
                        @"Lorenzo Evatt",
                        @"Erica Dozier",
                        @"Barbara Lazarus",
                        @"Joye Hocker",
                        @"Henry Arana",
                        @"Glen Cabrales",
                        nil];
            }

            - (id) initWithNames:(NSArray *)nameArray
            {
                self = [super init];
                if (self)
                {
                    delegates = nil;
                    allNames = [nameArray copy];
                    names = nil;
                    fakeSearchTimer = nil;
                    fakeSearchDuration = 0;
                }

                return self;
            }

            - (void) dealloc
            {
                TT_INVALIDATE_TIMER(fakeSearchTimer);
                TT_INVALIDATE_TIMER(fakeLoadingTimer)
                TT_RELEASE_SAFELY(allNames);
                TT_RELEASE_SAFELY(names);
                TT_RELEASE_SAFELY(delegates);
                
                [super dealloc];
            }

            // TTModel
            //
            - (NSMutableArray*) delegates
            {
                if (!delegates)
                {
                    delegates = TTCreateNonRetainingArray();
                }
                
                return delegates;
            }

            - (BOOL)isLoadingMore
            {
                return NO;
            }

            - (BOOL)isOutdated
            {
                return NO;
            }

            - (BOOL)isLoaded
            {
                return !!names;
            }

            - (BOOL)isLoading
            {
                return !!fakeSearchTimer || !!fakeLoadingTimer;
            }

            - (BOOL)isEmpty
            {
                return !names.count;
            }

            - (void)fakeLoadingReady
            {
                fakeLoadingTimer = nil;
                
                [self loadNames];
                
                [delegates perform:@selector(modelDidFinishLoad:) withObject:self];
            }

            - (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more
            {
                [delegates perform:@selector(modelDidStartLoad:) withObject:self];
                
                if (fakeLoadingDuration)
                {
                    TT_INVALIDATE_TIMER(fakeLoadingTimer);
                    fakeLoadingTimer = [NSTimer scheduledTimerWithTimeInterval:fakeLoadingDuration
                                                                        target:self
                                                                       selector:@selector(fakeLoadingReady)
                                                                      userInfo:nil
                                                                       repeats:NO];
                    [delegates perform:@selector(modelDidStartLoad:) withObject:self];
                } 
                else
                {
                    [self loadNames];

                    [delegates perform:@selector(modelDidFinishLoad:) withObject:self];
                }
            }

            - (void)invalidate:(BOOL)erase 
            {
            }

            - (void)cancel
            {
                if (fakeSearchTimer)
                {
                    TT_INVALIDATE_TIMER(fakeSearchTimer);
                    [delegates perform:@selector(modelDidCancelLoad:) withObject:self];
                }
                else if (fakeLoadingTimer)
                {
                    TT_INVALIDATE_TIMER(fakeLoadingTimer);
                    [delegates perform:@selector(modelDidCancelLoad:) withObject:self];    
                }
            }

            // public
            //
            - (void)loadNames
            {
                TT_RELEASE_SAFELY(names);
                names = [allNames mutableCopy];
            }

            - (void)search:(NSString *)text
            {
                [self cancel];
                
                TT_RELEASE_SAFELY(names);
                if (text.length)
                {
                    if (fakeSearchDuration)
                    {
                        TT_INVALIDATE_TIMER(fakeSearchTimer);
                        fakeSearchTimer = [NSTimer scheduledTimerWithTimeInterval:fakeSearchDuration
                                                                           target:self
                                                                          selector:@selector(fakeSearchReady:)
                                                                         userInfo:text
                                                                          repeats:NO];
                        [delegates perform:@selector(modelDidStartLoad:) withObject:self];
                    }
                    else
                    {
                        [self fakeSearch:text];
                        [delegates perform:@selector(modelDidFinishLoad:) withObject:self];
                    }
                }
                else
                {
                    [delegates perform:@selector(modelDidChange:) withObject:self];
                }
            }

            @end

            //
            // DataSource
            //
            @implementation KSDataSource

            @synthesize mockModel;

            - (id) init
            {
                self = [super init];
                if (self)
                {
                    mockModel = [[KSMockModel alloc] initWithNames:[KSMockModel fakeNames]];
                    self.model = mockModel;
                }
                return self;
            }

            - (void)dealloc
            {
                TT_RELEASE_SAFELY(mockModel);

                [super dealloc];
            }

            // UITableViewDataSource
            //
            - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
            {
                return [TTTableViewDataSource lettersForSectionsWithSearch:YES summary:NO];
            }

            - (void) tableViewDidLoadModel:(UITableView *)tableView
            {
                self.items = [NSMutableArray array];
                self.sections = [NSMutableArray array];
                
                NSMutableDictionary * groups = [NSMutableDictionary dictionary];
                for (NSString * name in mockModel.names)
                {
                    NSString * letter = [NSString stringWithFormat:@"%C", [name characterAtIndex:0]];
                    NSMutableArray * section = [groups objectForKey:letter];
                    if (!section)
                    {
                        section = [NSMutableArray array];
                        [groups setObject:section forKey:letter];
                    }
                    
                    TTTableItem * item = [TTTableTextItem itemWithText:name URL:nil];
                    [section addObject:item];
                }
                
                NSArray * letters = [groups.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
                for (NSString * letter in letters)
                {
                    NSArray * items = [groups objectForKey:letter];
                    [_sections addObject:letter];
                    [_items addObject:items];
                }
            }

            - (id<TTModel>) model
            {
                return mockModel;
            }

            @end

            然后修改 KSViewController.h為:
            #import <Three20/Three20.h>

            @interface KSViewController : TTTableViewController <TTTableViewDataSource, TTTableViewDelegate>

            @end

            KSViewController.m為:
            //
            //  KSViewController.m
            //  Three320Demo
            //
            //  Created by LuoZhaohui on 12/31/11.
            //  Copyright (c) 2011 kesalin@gmail.com. All rights reserved.
            //

            #import "KSViewController.h"
            #import "KSDataSource.h"

            @implementation KSViewController

            // TTTableView
            //
            - (void) createModel
            {
                KSDataSource *dataSource = [[KSDataSource alloc] init];
                dataSource.mockModel.fakeLoadingDuration = 2.0;
                dataSource.mockModel.fakeSearchDuration  = 2.0;
               
                self.dataSource = dataSource;
                [dataSource release];
            }

            - (id<TTTableViewDelegate>) createDelegate
            {
                TTTableViewDragRefreshDelegate *delegate = [[TTTableViewDragRefreshDelegate alloc] initWithController:self];
                
                return [delegate autorelease];
            }

            // TTTableViewDelegate
            //
            - (void)tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
            {
                
            }

            - (void)tableView:(UITableView *)tableView touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event
            {
                
            }

            @end

            現在編譯運行,你應該可以看到一個 loading 界面,2秒(代碼中有設定)之后進入 tableview 界面,在 tableview 中進行下拉操作,可以看到刷新功能以及在里面了!這個效果和我們使用下拉刷新庫 EGOTableViewPullRefresh 的效果是一樣的。
            posted on 2011-12-31 17:45 羅朝輝 閱讀(2779) 評論(0)  編輯 收藏 引用 所屬分類: 移動開發Cocoa 開發
            久久66热人妻偷产精品9| 久久国产香蕉一区精品| 久久国产色av免费看| 久久人人爽人人爽人人片AV不| 国产色综合久久无码有码| WWW婷婷AV久久久影片| 欧美激情精品久久久久久| 亚洲精品乱码久久久久久自慰| 久久精品国产99国产电影网| 欧美国产成人久久精品| 国产精品久久久久久搜索| 精品国产乱码久久久久软件| 久久91精品国产91久久小草| 一本色道久久综合| 香港aa三级久久三级| 亚洲AV日韩AV永久无码久久| 伊人丁香狠狠色综合久久| 亚洲色婷婷综合久久| 久久精品夜色噜噜亚洲A∨| 色综合久久中文字幕无码| 色婷婷久久综合中文久久一本| 久久国产精品77777| 色妞色综合久久夜夜| 久久伊人色| 亚洲国产成人久久一区WWW| 88久久精品无码一区二区毛片| 久久精品中文闷骚内射| 人人妻久久人人澡人人爽人人精品| 久久精品成人一区二区三区| 99久久精品国产毛片| 精品久久一区二区| 久久国产精品久久久| 欧美午夜精品久久久久免费视| 免费精品久久天干天干| 国产精品乱码久久久久久软件| 久久亚洲中文字幕精品一区四 | 久久国产免费观看精品3| 亚洲色大成网站WWW久久九九| 久久久久久久综合狠狠综合| 伊色综合久久之综合久久| 国产精品久久久久久久久久影院|