• [Cocoa]在工程中使用Three20库:下拉刷新 tableview


    [Cocoa]在工程中使用Three20库:下拉刷新 tableview

    本文遵循“署名-非商业用途-保持一致”创作公用协议

    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 的效果是一样的。

  • 相关阅读:
    HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))
    HDU 6330.Problem L. Visual Cube-模拟到上天-输出立方体 (2018 Multi-University Training Contest 3 1012)
    HDU 6326.Problem H. Monster Hunter-贪心(优先队列)+流水线排序+路径压缩、节点合并(并查集) (2018 Multi-University Training Contest 3 1008)
    杭电1518 Square(构成正方形) 搜索
    POJ1659 Frogs' Neighborhood(青蛙的邻居) Havel-Hakimi定理
    杭电1133 排队买票 catalan
    hdu 5945 Fxx and game 单调队列优化dp
    Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp
    hdu 4348 To the moon 主席树区间更新
    hdu 4417 Super Mario 树状数组||主席树
  • 原文地址:https://www.cnblogs.com/kesalin/p/cocoa_320.html
Copyright © 2020-2023  润新知