tags:

views:

178

answers:

2

I can't get my head round this. When the page loads, everything works fine - I can drill up and down, however 'stream' (in the position I have highlighted below) becomes not equal to anything when I pull up and down on the tableview. But the error is only sometimes. Normally it returns key/pairs.

If know one can understand above how to you test for // (int)[$VAR count]} key/value pairs in a NSMutableDictionary object

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *FirstLevelCell = @"FirstLevelCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
    }

    NSInteger row = [indexPath row];
    //NSDictionary *stream = (NSDictionary *) [dataList objectAtIndex:row];


    NSString *level = self.atLevel;
    if([level isEqualToString:@"level2"])
    {
        NSMutableDictionary *stream = [[NSMutableArray alloc] init];
        stream = (NSMutableDictionary *) [dataList objectAtIndex:row];

// stream value is (int)[$VAR count]} key/value pairs

        if ([stream valueForKey:@"title"] ) 
        {
            cell.textLabel.text = [stream valueForKey:@"title"];
            cell.textLabel.numberOfLines = 2;
            cell.textLabel.font =[UIFont systemFontOfSize:10];

            NSString *detailText = [stream valueForKey:@"created"];
            cell.detailTextLabel.numberOfLines = 2;
            cell.detailTextLabel.font= [UIFont systemFontOfSize:9];
            cell.detailTextLabel.text = detailText; 

            NSString *str = @"http://www.mywebsite.co.uk/images/stories/Cimex.jpg";
            NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];
            UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];

            cell.imageView.image = newsImage;

            [stream release];
        }
    }
    else 
    {
        cell.textLabel.text = [dataList objectAtIndex:row];
    }


    return cell;
}

Thanks for your time

A: 

I didn't really understand the question, but...

You can test for number of values in a dictionary by:

if ([[myDictionary allKeys] count] == someNumber) {
    // do something...
}
Paul Lynch
The code works fine but I can get 'myDictionary' to be (int)[$VAR count]} key/value pairs - I need to really test for this. This breaks with a EXC_BAD_ACCESS problem.
Frames84
+1  A: 

You are both leaking and over-releasing the stream dictionary:

NSMutableDictionary *stream = [[NSMutableArray alloc] init]; // <-- Create a new dictionary
stream = (NSMutableDictionary *) [dataList objectAtIndex:row]; // <-- Overwrite the reference with another dictionary. Previous dictionary is lost...

...

[stream release]; // <-- You are releasing an object you don't have the ownership.

You should remove the dictionary creation as it is useless and the release as you don't own the object.

Laurent Etiemble