【问题标题】:IPhone SDK, Display an NSMutable array in Table view?iPhone SDK,在表格视图中显示 NSMutablearray?
【发布时间】: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


【解决方案1】:

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

这将创建您的类的一个实例,当笔尖被解码时该实例将可用。

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

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

【讨论】:

    【解决方案2】:

    好吧,由于某种原因,有人碰到了这个旧线程。我不妨插话。这段代码有问题的原因是它试图将单元格的text 属性设置为NSNumber

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

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 2018-12-21
      相关资源
      最近更新 更多