【问题标题】:populate a table view using an array from core data使用核心数据中的数组填充表视图
【发布时间】:2012-07-21 09:34:19
【问题描述】:

我尝试从核心数据填充我的表,但在这一行出现错误

Tips *tipy = [arr objectAtIndex:indexPath.row];

这是我的 .m 文件

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [arr 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];
    }
    Tips *tipy = [arr objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [tipy tipdescription];
    cell.detailTextLabel.text = [tipy.tipnumber stringValue];

    return cell;
}

app 和 arr 在 .h 文件中被声明为并在 .m 文件中合成

@property (nonatomic, retain) AppDelegate *app;
@property(nonatomic, assign) NSArray *arr;

【问题讨论】:

  • 你有什么错误?你在你的文件中“#import Tips”了吗?你合成了属性吗?如果是这样,你应该写“self.arr”而不是arr

标签: iphone objective-c ios5 core-data


【解决方案1】:

正如moxy所说,你应该写self.arr而不是arr。你设置了@property(nonatomic, assaign) NSArray *arr;你合成了那些属性吗?一定要这样做。并尝试@property(nonatomic, retain) NSArray *arr; 而不是@property(nonatomic, assaign) NSArray *arr;。分配不会增加保留计数,因此有可能自动释放该值。不要忘记释放 dealloc 中的 arr 以避免内存泄漏。

【讨论】:

  • 非常感谢。问题是因为我没有在我的数组中使用保留
  • 如果这个答案对您有帮助,请接受。您应该根据Stackoverflow FAQ 接受答案
【解决方案2】:

只是猜测,因为没有足够的代码检查,但NSManagedObjectContextexecuteFetchRequest:error: 确实返回自动释放NSArray 并且您的属性arr 定义为assign,而不是strong/retain => 你的NSArray 被释放并且arr 指向垃圾。也许这就是原因,但正如我所说,没有足够的代码可以确定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多