【发布时间】:2010-05-30 13:23:56
【问题描述】:
我尝试按照教程在表格视图中显示我的 NSMutableArray。由于某种原因它完全失败了,我想我知道为什么但无法解决它,这是我的代码:
- (void) scoreSystem {
scoreArray = [[NSMutableArray alloc] init];
NSNumber *onescore = [NSNumber numberWithInteger:score];
[scoreArray addObject:onescore];
NSNumber *twoscore = [NSNumber numberWithInteger:score];
[scoreArray addObject:twoscore];
NSNumber *threescore = [NSNumber numberWithInteger:score];
[scoreArray addObject:threescore];
NSNumber *fourscore = [NSNumber numberWithInteger:score];
[scoreArray addObject:fourscore];
NSNumber *fivescore = [NSNumber numberWithInteger:score];
[scoreArray addObject:fivescore];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [scoreArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [scoreArray objectAtIndex:indexPath.row];
return cell;
}
我认为这是因为它不会让我在 IB 中正确链接所有内容,它让我将数据源和委托给文件所有者,但是当我从文件所有者拖动到我的视图时,它显示“委托”而不是“视图”,我认为这是因为我在“主窗口”而不是 VC 中进行操作。 有没有办法解决这个问题? 谢谢! 哈利。
【问题讨论】:
-
viewDidLoad 中是否调用了 scoreSystem?试试
scoreArray = [[[NSMutableArray alloc] init] retain]; -
请不要这样尝试。 alloc / init 将保留数组,额外的保留会导致泄漏。
-
在 numberOfRowsInSection 和 cellForRowAtIndexPath 中放置断点或 NSLog 以确保它们被调用。还要确保 numberOfSectionsInTableView 返回一个非零值。如果这些没有被调用,请确保您的表视图数据源正确连接到您的 TableViewController。回来报告。 :)
-
汤姆,你是对的。我不知道我在想什么。
-
我完全不明白为什么它不起作用,所以我将尝试以不同的方式显示数组。有谁知道在纯文本视图或其他内容中显示 NSMutableArray 的方法? :/ 谢谢哈利
标签: iphone uitableview sdk nsmutablearray