开发者

Iphone xcode simulator crashes when I move up and down on a table row

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] au开发者_如何学Pythontorelease];
    }

    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


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.


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...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜