Cocoa Touch表数据源问题
我在显示数据源的结果时遇到一些问题。 这段代码将在控制台中显示不同的(且正确的)结果,但会在模拟器中产生各种随机的垃圾。
(“results”是该类的 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;
}
I'm having some trouble with displaying results from a datasource. This code will show a different (and correct) result in the console, but results in all kinds of random crap in the simulator.
("results" is an NSMutableArray property for the class.)
-(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];
}
I'm guessing that this might have something to do with memory retention of the array, or the strings in the array? I'm afraid I still haven't got the hang of that.
[edit in response to Marc Bessey -- I think everything here is your basic datasource methods]
-(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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为问题出在您发布的代码中。 它更有可能出现在为表视图实现数据源的代码中。
I don't think the problem is in the code that you've posted. It's more likely in the code that implements the datasource for your table view.