【问题标题】:Cocoa Touch table datasource problemCocoa Touch 表数据源问题
【发布时间】:2009-03-31 03:44:30
【问题描述】:

我在显示来自数据源的结果时遇到了一些问题。此代码将在控制台中显示不同(且正确)的结果,但会在模拟器中导致各种随机废话。

(“结果”是该类的 NSMutableArray 属性。)

-(void) handleSearchForKeywords: (NSString *) keywords {
    [results removeAllObjects];
    int r = rand() % 10;
    for( int i = 0; i < r; i++ ) {
        [results addObject:[NSString stringWithFormat:@"test %i: %@", i, keywords]];
    }
    [self reloadTheTable];
}

-(void) reloadTheTable {
    NSLog( @"current array contents: %@", results );
    [tableView reloadData];
}

我猜这可能与数组的内存保留或数组中的字符串有关?恐怕我还没有掌握这个窍门。

[编辑以回应 Marc Bessey -- 我认为这里的一切都是您的基本数据源方法]

-(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section {
    return [results count];
}

-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
    static NSString *SearchViewControllerCell = @"SearchViewControllerCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SearchViewControllerCell];
    if( cell == nil ) {
        cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: SearchViewControllerCell] autorelease];
        NSUInteger row = [indexPath row];
        [cell setText:[results objectAtIndex:row]];
    }
    return cell;
}

【问题讨论】:

    标签: iphone cocoa-touch datasource nsmutablearray retain


    【解决方案1】:

    我认为问题不在于您发布的代码。它更有可能在为您的表视图实现数据源的代码中。

    【讨论】:

    • 你是对的。设置单元格文本的位在缓存循环内。 if(cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: blah] autorelease]; [单元格设置文本:[结果 objectAtIndex:行]]; } ^ 错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多