问题描述
- UITableView从jsonURL加载数据
-
在UITableview的JSON的URL绑定数据,但是在执行过程中报错。NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; statuses = [parser objectWithString:json_string error:nil]; [self.dropdownTblView reloadData]; for (NSDictionary *status in statuses) { _altTitle = [status valueForKey:@"Title"]; NSLog(@"Title %@",_altTitle); } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSLog(@"%d",[statuses count]); return [statuses 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]; } return cell; //Here I'm getting an error id obj = [statuses objectAtIndex:indexPath.row]; NSString *name = [obj valueForKey:@"Title"]; cell.textLabel.text =name; return cell; }
JSON内容:
[ { "Id": 1, "Title": "Tamil to English", "AltTitle": "??§|??????|??????| |??????|??≤|??????| |???|??????|??????|??≤|??????", "Description": "Learn English through Tamil", "Code": 1, "Version": "1.0", "SourceLanguageIndicator": "???", "TargetLanguageIndicator": "A", "SourceLanguageCode": "ta", "TargetLanguageCode": "en", "Updated": "2013-02-21T03:33:19.6601651+00:00" } ]
解决方案
上面的代码在未执行到你报错的地方的时候,就返回了吧
return cell; //这一句直接就返回了.不会执行下面的代码.
//Here I'm getting an error
id obj = [statuses objectAtIndex:indexPath.row];
解决方案二:
在同一范围内你返回了两次cell。删除第一次t return cell。并且你在for循环之前调用reloadData 。可能会造成表中的数据源是空的。换成在for循环之后调用reloadData。
时间: 2024-10-07 05:30:26