没有调用TTModel加载方法

时间:2011-05-15 19:21:24

标签: cocoa-touch ios ios4 three20

问题

描述

以下代码导致 TTModel 加载方法未被调用。我已经通过调试器,以及单步执行TTCatalog应用程序。我能看到的两者之间的唯一区别是,当目录在控制器的 createModel 方法中设置它的DataSource时,TTModel的加载方法被调用,而我的不

关于守则

我已经评论了代码的特定区域,告诉他们应该做什么以及发生了什么问题,但为了完成起见我将所有内容都包括在内。

你应该具体看看

  • PositionsController的 createModel 方法
  • PositionsList的加载方法

这些是问题所在的领域,也是最佳起点。


代码

PositionsController:TTTableViewController

- (id)initWIthNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        self.title = @"Positions";
        self.variableHeightRows = NO;
        self.navigationBarTintColor = [UIColor colorWithHexString:@"1F455E"];
    }

    return self;
}

//This method here should result in a call to the PositionsList load method
- (void)createModel
{
    PositionsDataSource *ds = [[PositionsDataSource alloc] init];
    self.dataSource = ds;
    [ds release];
}

- (void)loadView
{
    [super loadView];
    self.view = [[[UIView alloc] initWithFrame:TTApplicationFrame()] autorelease];
    self.tableView = [[[UITableView alloc] initWithFrame:TTApplicationFrame() style:UITableViewStylePlain] autorelease];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"E2E7ED"];
    self.tableView.separatorColor = [UIColor whiteColor];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}

//Override UITableViewDelegate creation method, so we can add drag to refresh
- (id<TTTableViewDelegate>) createDelegate {

    TTTableViewDragRefreshDelegate *delegate = [[TTTableViewDragRefreshDelegate alloc] initWithController:self];

    return [delegate autorelease];
}

PositionsDataSource:TTListDataSource

@implementation PositionsDataSource

@synthesize positionsList = _positionsList;

-(id)init
{
    if (self = [super init]) 
    {
        _positionsList = [[PositionsList alloc] init];
        self.model = _positionsList;
    }
    return self;
}

-(void)dealloc
{
    TT_RELEASE_SAFELY(_positionsList);
    [super dealloc];
}

-(void)tableViewDidLoadModel:(UITableView*)tableView
{
    self.items = [NSMutableArray array];
}

-(id<TTModel>)model
{
    return _positionsList;
}
@end

PositionsList:NSObject <TTModel&gt;

@implementation PositionsList

//============================================================
//NSObject

- (id)init
{
    if (self = [super init])
    {
        _delegates = nil;
        loaded = NO;
        client = [[Client alloc] init];
    }
    return self;
}

- (void)dealloc
{
    TT_RELEASE_SAFELY(_delegates);
    [client dealloc];
    [super dealloc];
}

//==============================================================
//TTModel

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

-(BOOL)isLoadingMore
{
    return NO;
}

-(BOOL)isOutdated
{
    return NO;
}

-(BOOL)isLoaded
{
    return loaded;
}

-(BOOL)isEmpty
{
    //return !_positions.count;
    return NO;
}

-(BOOL)isLoading
{
    return YES;
}

-(void)cancel
{
}

//This method is never called, why is that?
-(void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more
{
    //This method is not getting called
    //When the PositionsController calls self.datasource, load should be called,
    //however it isn't.
    [_delegates perform:@selector(modelDidStartLoad:) withObject:self];
    [client writeToServer:self dataToSend:@""];
}

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

@end

1 个答案:

答案 0 :(得分:1)

简短回答:在您的PositionList中为 isLoading 返回 NO 而不是 YES

更长的解释:

如果您浏览Three20源代码,您会发现在视图控制器上设置dataSource会设置模型,刷新模型并可能调用负载。以下是TTModelViewController刷新时调用的代码:

- (void)refresh {
  _flags.isViewInvalid = YES;
  _flags.isModelDidRefreshInvalid = YES;

  BOOL loading = self.model.isLoading;
  BOOL loaded = self.model.isLoaded;
  if (!loading && !loaded && [self shouldLoad]) {
    [self.model load:TTURLRequestCachePolicyDefault more:NO];

  } else if (!loading && loaded && [self shouldReload]) {
    [self.model load:TTURLRequestCachePolicyNetwork more:NO];

  } else if (!loading && [self shouldLoadMore]) {
    [self.model load:TTURLRequestCachePolicyDefault more:YES];

  } else {
    _flags.isModelDidLoadInvalid = YES;
    if (_isViewAppearing) {
      [self updateView];
    }
  }
}

您的PositionList对象为isLoading返回YES,为isLoaded返回NO。这意味着Three20认为你的模型在没有加载时正在加载。如果默认情况下不返回YES,您可能还需要实现shouldLoad。