- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 NSMutableArray *rows;
,我在 viewDidLoad
中初始化并填充数据。到那时,显然它已经有了数据。在本例中,为三个条目。
然后在 tableView:cellForRowAtIndexPath
中,我调用 [rows objectAtIndex:indexPath.row]
。但是,此时 rows
数组仍包含三个条目,但这些条目的值为 0x00000000
而不是原始值(例如“id”为 12345
但现在是 0x00000000
。
在我看来,rows
中的数据值在 viewDidLoad
和 tableView:cellForRowAtIndexPath
之间的某处被清空。可能是什么原因造成的?
编辑
这是代码:
ViewController.m
:
@implementation ViewController
NSMutableArray *rows;
- (void)viewDidLoad
{
rows = [[NSMutableArray alloc] init];
[rows setArray:myData]; // myData is als an NSMutableArray populated from JSON data.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
User *user = [rows objectAtIndex:indexPath.row]; // At this point 'rows' contains three entries but the values are empty.
}
@end
编辑2
以下是经过几次建议更改后的代码:
ViewController.m
@interface ViewController()
{
NSMutableArray *rows;
}
@implementation ViewController
- (void)setRowsFromJSON
{
NSString *fileContents = [NSString stringWithContentsOfFile:@"data.json" encoding:NSUTF8StringEncoding error:nil];
NSData *jsonData = [fileContents dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
rows = [NSMutableArray arrayWithCapacity:[jsonArray count]];
User *user;
for (NSDictionary *aUser in jsonArray) {
user = [[User alloc] init];
user.id = [aUser valueForKey:@"id"];
user.name = [aUser valueForKey:@"name"];
[rows addObject:user];
}
}
- (void)viewDidLoad
{
[self setRowsFromJSON];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
User *user = [rows objectAtIndex:indexPath.row]; // At this point 'rows' contains three entries but the values are empty.
}
@end
最佳答案
0x000000000 为零。这意味着该行没有指向任何地方。
我在 iOS 5.1 上运行应用程序时遇到了同样的问题。问题是您假设 viewDidLoad 始终在 tableview:cellForRowAtIndexPath: 之前调用。但事实并非一定如此。第一个是 View Controller 的方法。第二个是Table View Data Source 的方法。两者是同一个对象这一事实纯属偶然。
就我而言, TableView Controller 方法在 viewDidLoad 之前调用,然后在 viewDidLoad 之后再次调用,因为对 View 表属性的某些修改总是会重新加载。
尝试初始化,例如 - numberOfSectionsInTableView:
关于ios - 为什么 UITableView 的 NSMutableArray 数据在被 tableView 使用时丢失 :cellForRowAtIndexPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16242341/
我有一个问题,当从 tableView:cellForRowAtIndexPath 调用时,cellForRowAtIndexPath 总是返回 nil: 问题是我实际上是在 tableView:ce
假设 self.tableView 是我创建的 UITableView 实例变量,任何人都可以解释这两个语句之间的区别吗?两者在语法上都是正确的,而且我认为两者都会做同样的事情:在 self.tabl
我对 iPhone 开发还很陌生。因此,如果我问一些非常简单的问题,请耐心等待。 在我的应用程序中,我有多个 View (即 .xib 文件)。单击主视图(CouponWebsiteViewContr
Application Specific Information: *** Terminating app due to uncaught exception 'NSRangeException',
我正在显示通过 Storyboard 初始化的动态表格 View 。 没什么特别的,只是用文本标签初始化 3 个单元格。 以下是实际发生的情况: 2014-08-15 12:39:19.909 Pop
我正在开发一个应用程序,我有一个 UITableView,它使用 tableView 方法填充了一个 NSMutableArray:cellForRowAtIndexPath:,它工作正常,但问题是我
我在两个 View 之间沟通时遇到了麻烦。我的目标是向我的 NSMutableArray 添加一个新对象.感谢 stackoverflow 上可爱的人,他们已经修复了。现在我有另一个问题。 即使我能够
我有一个奇怪的错误。我有一个带有静态单元格的分组 UITableView,我想在每个部分下添加一个白色阴影。我考虑的方法是为一个部分中的每个最低单元格添加阴影。 所以在 viewDidAppear 中
每当我滚动我的 tableview 时,它都非常滞后。我认为这与我加载细胞的方式有关。我尽可能使用 UINib (5.0+),同时仍然提供向后兼容性。然后我从 NSArray 中的 NSDiction
我不明白cellForRowAtIndexPath函数的indexPath参数始终为0。我的表当前有7行。我的表格显示的是表格第一行的7倍。是什么导致indexPath始终为零? @implement
我正在开发一个使用表格 View 的 ToDoList iOS 应用程序。单元格内容(UITableCellView)由 nsmutablearray 的实例变量填充。 当我添加一个新的 ToDoIt
所以我有快速的代码,可以创建一个配置文件列表,并允许个人对其进行过滤和排序以及单击它们。就像地址簿一样。它以主/详细信息格式设置,其中主信息是人员列表,详细信息是个人。我还有一个来自 master 的
完整代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
我有两个实体:日记帐和帐户。这种关系是一对多的:Journal 只有一个 Account。账户有很多日记账。我想根据每个帐户的日记显示总计,但我不知道如何操作。目前我为每个帐户获得的总额是相同的: 在
我的 cellForRowAtIndexPath 并非每次都会触发我将 dataSource 和 delegate 设置给文件所有者。我还设置了引用 socket 。我还尝试了 tbl.delegat
我的项目: 我的项目中有一个 UITableView。每个 UITableViewCell 都有一个 UITextView 作为 subview 。当用户点击“添加”按钮时,我需要再添加一个带有 UI
我的单元格中的数据在 cellforrowatindexpath 中混淆了。我更改了标签的文本颜色和文本,但即使 isComplete 值更改,单元格颜色和文本也会随机更改。 例如,当我滚动表格 Vi
免责声明:我使用的是 iOS 8,因此这可能是一个错误。 我正在尝试在特定事件发生后以编程方式编辑 UITableView 中特定 IndexPath 处单元格的背景颜色。我使用以下代码: UITab
填充 TableView 时出现奇怪的错误。这是我做过无数次的事情,但现在我对一些可能非常简单的事情感到困惑。也许我只是醒得太久了。 我有一个名为 riverGauge 的对象,它负责从 Web 服务
我的应用程序中有一个 tableView,当用户点击该单元格时,我需要它转到另一个 View Controller ,该 Controller 提供有关他们单击的单元格的更多详细信息。当我尝试使用 获
我是一名优秀的程序员,十分优秀!