不显示的UITableViewController单元格文本标签内容(UItableviewcont

2019-10-17 01:33发布

我已经成功地解析JSON文件在我的应用程序,但是当我试图显示它的所有表查看其没有得到显示。这里是我的代码。

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

Answer 1:

我没有看到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"]];

此外,请确保您的委托返回段的正确数量为你的餐桌。



Answer 2:

在的tableview部分的数量至少是一个...

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;//It should be at least one.......
    }

写在下面的代码cellForRowAtIndexPath:方法。 否则,你会得到错误。

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

我认为这将是对你有所帮助。



Answer 3:

调用[tableView reloadData]您添加常用3表格数组中后...

[tablearray addObject:arr];
[tableView reloadData]
NSLog(@"2");

希望能帮助到你



Answer 4:

你应该初始化像这样的小区:

- (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];

    }
    [[cell textLabel] setText:[tablearray objectAtIndex:indexPath.row]];

    // Configure the cell...

    return cell;
}


Answer 5:

你有tablarray 1元。 该元素是包含几个条目的阵列。 您的数据源只声明了一个单元(tablearray算,这是一个)。

你cellForIndexPath方法总是会第一个也是唯一的第一个元素的JSON数组中的反正。

编辑:无关,但如果在你的JSON字段没有设置,你会得到零回来,那会终止您在arrayWithObjects阵列,这可能会导致出界指数向下行的。 要非常小心使用这种方法,它很容易拍摄自己与它的脚。



文章来源: UItableviewcontroller cell text label content not displayed