0

今天我遇到了无法复制的错误,这让我很困惑。好的小背景:

我目前正在开发具有标签栏控制器作为初始视图控制器的应用程序。有几个导航控制器连接到不同的标签栏项目。

其中之一是从 JSON 填充的 tableViewController。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *simpleTableIdentifier = @"SimpleTableItem";
   SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}   

     //NSMutableArray for storing loaded values
[pics addObject:imageLoad];
[names addObject:[aucdict objectForKey:@"name"]];
[idcka addObject:[aucdict objectForKey:@"auction_id"]];

  // Configure the cell...
cell.nameLabel.text = [aucdict objectForKey:@"name"];
cell.priceLabel.text = [NSString stringWithFormat:@"%@",priceString];
cell.timeLabel.text = [NSString stringWithFormat:@"%@",timeString];

cell.thumbnailImageView.image = imageLoad;



return cell;}

单击行后,我执行 performSegueWithIdentifier:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"showAuctionDetail" sender:self];}

在 prepareForSegue 我发送一些数据到下一个 ViewController

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

{

namesArray = [[NSArray alloc] initWithArray:names];
picsArray =[[NSArray alloc] initWithArray:pics];
IDarray = [[NSArray alloc] initWithArray:idcka];
if ([segue.identifier isEqualToString:@"showAuctionDetail"])
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    detailViewController *dViewController = segue.destinationViewController;
    dViewController.selectedAuctionTitle = [namesArray objectAtIndex:indexPath.row];
    dViewController.auctionPic = [picsArray objectAtIndex:indexPath.row];
    dViewController.id_aukcie = [IDarray objectAtIndex:indexPath.row];
}}

Now comes my problem. Sometimes (this really confuses me, because it I haven't found when does it happen) when I start the application, and tap on some row, I get totally different data passed to DetailViewController. The only thing, I can guess is that my arrays are different (they contain more or less values) than actual JSON response. But that would mean, my app would crash if I clicked on first or last item in table (index out of bounds or something like that), that never happened.

I've seen this bug happened maybe 5 times randomly. I tried to run and quit app for 20 times in a row and it happened only once.

P.S. I know that class name (detailViewController) should start with capital letter, I apologize for that :)

edited: as rdelmar suggested

4

1 回答 1

1

I think the problem is having the call out to the server in the cellForRowAtIndexPath: method. You should put that code in viewDidLoad, and then when the data has come back, and is finished parsing, call reloadData on your table view.

于 2012-11-08T17:06:55.527 回答