IPhone SDK,在Table视图中显示NSMutablearray?

时间:2010-05-30 13:23:56

标签: iphone uitableview sdk nsmutablearray

我已经尝试按照教程在表视图中显示我的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。 这有什么办法吗? 谢谢! 哈里。

2 个答案:

答案 0 :(得分:0)

您希望将代码所在的类设置为tableview的数据源。在IB中创建类的实例(使用NSObject,并将其类重命名为YourClass)。

这将创建一个在解码nib时可用的类实例。

然后,从tableview控制拖动到您的类,并设置数据源。

就是这样!您应该能够在上面的-numberOfRowsInSection:方法中设置断点,并在表视图进入视图时立即调用它。如果不这样做,请检查您的连接并检查拼写错误:运行时区分大小写。

答案 1 :(得分:0)

出于某种原因,有人碰到了这个老线程。我可能也会参与进来。这段代码出现问题的原因是因为它试图将单元格的text属性设置为NSNumber

cell.textLabel.text = [scoreArray objectAtIndex:indexPath.row];

请改为尝试:

cell.textLabel.text = [[scoreArray objectAtIndex:indexPath.row] stringValue];
相关问题