0

I'm new to objective-c programming and I'm writing application which uses XML files as data source. I've got class Score with init:

- (id)initWithPlayer1Name:(NSString *)name1 Player2Name:(NSString *)name2 Player1Url:(NSString *)url1 Player2Url:(NSString *)url2 Player1FrameScore:(NSString *)frame1 Player2FrameScore:(NSString *)frame2 Player1InFrameScore:(NSString *)score1 Player2InFrameScore:(NSString *)score2
{
    if(self = [super init])
    {
        self.name1 = name1;
        self.name2 = name2;
        self.url1 = url1;
        self.url2 = url2;
        self.image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url1]]];
        self.image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url2]]];
        self.frame1 = frame1;
        self.frame2 = frame2;
        self.score1 = score1;
        self.score2 = score2;
    }
    return self;
}

And I want to display list of scores in table view. When application starts there are some sample data and table view with my custom cell (display 2 names and 2 images from score class in every row) works fine. Problems begin when I update data from XML (I'm using XMLParser - works well). Players names are updated, but images disappears. In other table view I've got list of players in custom cell but with only one picture and it works fine.

Here is code from scores table view class:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ScoresCell";
    ScoresCell *cell = (ScoresCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = (ScoresCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    Score *score = [self.scores objectAtIndex:indexPath.row];
    cell.player1Name.text = score.name1;
    cell.player1Image.image = score.image1;
    cell.player2Name.text = score.name2;
    cell.player2Image.image = score.image1;
    return cell;
}

I was testing it in many ways, but I have not found a solution.

Red Devil
  • 1
  • 2

2 Answers2

0

First thing if you reuse cells you have to consider two cases in cellForRowAtIndexPath:

  1. dequeueReusableCellWithIdentifier return a cell
  2. dequeueReusableCellWithIdentifier returns nil. In this case you have to allocate a new cell.

Out of this if block (so when you have a cell either reused or a new one) you will setup the values of each element so labels and images for example. In the case of your code all code under the dequeueReusableCellWithIdentifier code line.

Follow what written in this other stackoverflow thread: [question] initWithFrame : reuseIdentifier : is deprecated where you will find the if( cell == nil ) code block...

After that if you still have problem post it again :)

Community
  • 1
  • 1
Esses77
  • 86
  • 2
  • 7
  • I did it this way but unfortunately it doesn't solve the problem. – Red Devil Jul 25 '12 at 08:24
  • Okay leave the code in this way that is the right one and now the problem is out of your cell code... Do you the reloadData after xml downloading and your Scores array updating? – Esses77 Jul 25 '12 at 08:37
  • Yes, because on start I've got sample data and when I update data from XML players names are changeing (and default pictures disappears). In other class I've got players list with images and it works fine from sampla data and also from XML. Reload is here: - (void)viewWillAppear:(BOOL)animated { self.scores = [dataUpdater updateScores]; [self.tableView reloadData]; } – Red Devil Jul 25 '12 at 08:43
  • Is updateScores in the same thread and so ui hangs during updating or not? Because if not reloaData will be called before all images are downloaded and you will have no images in some cases... – Esses77 Jul 25 '12 at 08:50
  • I've got button in other view and when I'll click it app is parsing xml file, then I go to other view (by tabBar) and there I see data, so data are updated (as I wrote, in other view it works fine in a similar way). dataUpdater is a singleton in which I've got arrays of objects(players, scores...). It's strange... – Red Devil Jul 25 '12 at 08:55
  • Put one NSLog before the reloadData and one after updating end in the singleton code. If the reloadData NSLog appears before the update end one the problem is here... – Esses77 Jul 25 '12 at 09:19
  • Update works fine (I've tested it). Before update I've got sample data: http://i48.tinypic.com/vdicck.png after update I've got: http://i49.tinypic.com/30wxmhy.png. As you see it almost works, only images are missing. – Red Devil Jul 25 '12 at 09:36
0

Next time I'll first check my own XML files. There were white characters in one file and that was a problem.

Red Devil
  • 1
  • 2