objective-c - UItableviewcontroller 单元格文本标签内容不显示

标签 objective-c ios xcode uitableview

我已经在我的应用程序中成功解析了 json 文件,但是当我尝试在表格 View 中显示所有文件时,它没有显示出来。这是我的代码。

NSString *urlstr=[NSString stringWithFormat:@"http://minora.p41techdev.net/portal.php"];

NSURL *url=[NSURL URLWithString:urlstr];

NSData *data =[NSData dataWithContentsOfURL:url];

NSError *error;

NSArray *json=(NSMutableArray*) [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

//NSLog(@"%@",json);
NSDictionary *dict =[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"a title", @"more data",nil] forKeys:[NSArray arrayWithObjects:@"titleKey",@"dataKey", nil]];


NSLog(@"1");

NSString *integ = [dict valueForKey:@"id"];
NSString *title=[dict valueForKey:@"title"];
NSString *category=[dict valueForKey:@"category"];
NSString *description=[dict valueForKey:@"description"];
NSString *spectitle=[dict valueForKey:@"spectitle"];
NSString *specvalue=[dict valueForKey:@"specvalue"];

NSArray *arr =[NSArray arrayWithObjects:integ,title,category,description,spectitle,specvalue, nil];

[tablearray addObject:arr];    
NSLog(@"%@",tablearray);





}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [tablearray count];
NSLog(@"5");

}


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


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


    return cell;

}

我的 json 文件看起来像这样
[
{
    "id": "1",
    "category": "New Category1",
    "title": "New Product2",
    "description": "Please type a description1",
    "imgurl": "http://s.wordpress.org/about/images/wordpress-logo-notext-bg.png",
    "spectitle": "Specification",
    "specvalue": "Value"
},
{
    "id": "2",
    "category": "Mobile",
    "title": "Samsung",
    "description": "Please type a description",
    "imgurl": "http://s.wordpress.org/about/images/wordpress-logo-notext-bg.png",
    "spectitle": "Price",
    "specvalue": "100$"
}
]

请指导...

我遇到这样的线程问题
2012-07-20 19:36:03.504 tablevc[2253:f803] 1
2012-07-20 19:36:03.505 tablevc[2253:f803] 2
2012-07-20 19:36:03.507 tablevc[2253:f803] 4
2012-07-20 19:36:03.507 tablevc[2253:f803] 3
2012-07-20 19:36:03.508 tablevc[2253:f803] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-07-20 19:36:03.508 tablevc[2253:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(0x13d2022 0x1563cd6 0x137aa48 0x9b32cb 0xb6d28 0xb73ce 0xa2cbd 0xb16f1 0x5ad21 0x13d3e42    0x1d8a679 0x1d94579 0x1d194f7 0x1d1b3f6 0x1d1aad0 0x13a699e 0x133d640 0x13094c6 0x1308d84  0x1308c9b 0x12bb7d8 0x12bb88a 0x1c626 0x2ae2 0x2a55 0x1)
 terminate called throwing an exception

最佳答案

我在任何地方都没有看到 tablearray 的初始化。

将此添加到您的 viewDidLoad 方法中:

tablearray = [[NSMutableArray alloc] init];

我还看到您在数组中插入了一个数组。这意味着当您需要访问正确的数据(在您的情况下为 NSString)时,您必须使用正确的索引:
cell.textLabel.text=[[tablearray objectAtIndex:indexPath.row] objectAtIndex:1];   

我使用了“objectAtIndex:1”,因为标题字符串存储在内部数组的索引 1 处。更好、更通用的方法是使用 NSDictionary。例如:
NSDictionary *dict =[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"a title", @"more data",nil] forKeys:[NSArray arrayWithObjects:@"titleKey",@"dataKey"]];

此外,请确保您的代表为您的表格返回正确数量的部分。

关于objective-c - UItableviewcontroller 单元格文本标签内容不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11580367/

相关文章:

iphone - c中的函数名称范围

ios - 在 UITableView 的自定义单元格中引用 UITextFields

objective-c - 释放 iOS 中未使用的属性

objective-c - 暂时禁用热角和停靠屏幕切换

ios - 将图像数组传递给 IOS 中的 slider

ios - 不同iOS版本之间的应用程序迁移

ios - NSFetchedResultsController 对空结果的自定义行为

xcode - xcode7 调试器发生了什么变化,我无法再使用 "po"/Swift 查看变量

ios - iOS 13 中未调用应用程序委托(delegate)方法

ios - 如何在 Swift 中呈现 iOS UIActionSheet?