开发者

TTModel load method not being called

Issue

Description

The following code results in load method of TTModel not being called. I've stepped it through the debugger, as well as stepping through the TTCatalog application. The only difference between the two that I can see, is that when the catalog sets it's DataSource in the createModel method of the controller, TTModel's load method gets called, whereas mine does not.

About the Code

I've commented the specific areas of code, to tell what they should be doing and what problem is happening, but I included all of it for completion's sake.

You should look at specifically

  • PositionsController's createModel method
  • PositionsList's load method

Those are the areas where the issue is, and would be the best place to start.


Code

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>

@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;
}开发者_C百科

-(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


Short answer: return NO instead of YES for isLoading in your PositionList.

For a longer explanation:

If you dig through the Three20 source, you'll find that setting the dataSource on a view controller sets the model, refreshing the model and potentially calling load. Here is the code called when the TTModelViewController refreshes:

- (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];
    }
  }
}

Your PositionList object is returning YES for isLoading and NO for isLoaded. This means that Three20 thinks your model is loading when it isn't. You may also need to implement shouldLoad if it doesn't return YES by default.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜