gpt4 book ai didi

cocoa-touch - TTModel 加载方法未被调用

转载 作者:行者123 更新时间:2023-11-29 05:04:14 24 4
gpt4 key购买 nike

问题

描述

以下代码导致 TTModelload 方法未被调用。我已单步执行调试器以及 TTCatalog 应用程序。我可以看到两者之间的唯一区别是,当目录在 Controller 的 createModel 方法中设置其数据源时,TTModel 的 load 方法被调用,而我的则被调用不是。

关于代码

我对代码的特定区域进行了注释,以告诉他们应该做什么以及发生了什么问题,但为了完整起见,我将所有内容都包含在内。

你应该具体看看

  • PositionsController 的 createModel 方法
  • PositionsList 的 load 方法

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

<小时/>

代码

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

位置列表: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;
}

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

最佳答案

简短回答:对于您的 PositionList 中的 isLoading,返回 NO 而不是 YES

更详细的解释:

如果您仔细研究 Three20 源代码,您会发现在 View Controller 上设置数据源会设置模型,刷新模型并可能调用加载。以下是 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。

关于cocoa-touch - TTModel 加载方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6010640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com