objective-c - 来自 NSSet 的 NSArray 元素未显示在 TableViewCell 中

标签 objective-c ios xcode uitableview nsarray

我有从 NSSet 实现的 NSArray 元素,如果我试图在 Table View Cell 中显示元素,我会在 tableView numberOfRowsInSection 部分遇到 BAD ACCESS 问题。这是我的代码

- (void)viewDidLoad
{
[super viewDidLoad];




jsonurl=[NSURL URLWithString:@"http://www.sample.net/products.php"];//NSURL

jsondata=[[NSString alloc]initWithContentsOfURL:jsonurl];//NSString
jsonarray=[[NSMutableArray alloc]init];//NSMutableArray

self.jsonarray=[jsondata JSONValue];

array=[jsonarray valueForKey:@"post_title"];

set = [NSSet setWithArray:array];//NSMutableSet
array=[set allObjects];//NSArray


NSLog(@"%@",array);



}


#pragma mark - Table view data source

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)  
{        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];



}
// Configure the cell...

cell.textLabel.text = [self.array objectAtIndex: [indexPath row]];    
return cell;
}

请提供帮助。提前致谢。

最佳答案

在您的代码中,您没有分配数组。您正在为该数组设置一个自动释放的对象,这就是您收到此错误的原因。

array=[set allObjects]; 替换为 array=[[set allObjects] retain];

关于objective-c - 来自 NSSet 的 NSArray 元素未显示在 TableViewCell 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11771867/

相关文章:

ios - 在layoutSubviews中设置 subview 框架不会持续存在

ios - 将 Core Data 对象从 DetailViewController 传递到另一个 View Controller

xcode - Mac 10.6+ 和 Xcode 4+ 上的 CPU Affinity

ios - 很快,有没有办法让 UIView 类也成为 MPMediaPickerControllerDelegate?

iOS 将日期转换为字符串 AM/PM

objective-c - 带有自定义阴影的 NSWindow

objective-c - CFP 引用/库/首选项

ios - 如何将 SwiftUI View 动态添加到父 View ?

iOS可变数组始终为空

ios - UITouch坐标和SKSpriteNode坐标不同