NSMutableArray在ViewDidLoad中工作,但在DidSelectRowAtIndexPath中不起作用

时间:2011-09-21 22:39:24

标签: cocoa-touch nsmutablearray

Menu.h

@interface Menu : UITableViewController {    
    NSMutableArray *arrayCellCollectionOrder;
    NSMutableDictionary *dictCellCollection;
    NSMutableDictionary *dictCellIndividual;
}

@property (nonatomic, retain) NSMutableArray *arrayCellCollectionOrder;

@end

Menu.m

ViewDidLoad正常工作。

@synthesize arrayCellCollectionOrder;

- (void)viewDidLoad {

    // Codes to read in data from PLIST
    // This part works

    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    plistPath = [rootPath stringByAppendingPathComponent:@"InfoTableDict.plist"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:@"InfoTableDict" ofType:@"plist"];
    }

    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                        propertyListFromData:plistXML
                                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                        format:&format
                                        errorDescription:&errorDesc];

    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }

    arrayCellCollectionOrder = [[[NSMutableArray alloc] init] retain];
    arrayCellCollectionOrder = [temp objectForKey:@"CellCollectionOrder"]; 

    // I can access `arrayCellCollectionOrder` here, it's working.

}

cellForRowAtIndexPath正常工作。我可以访问arrayCellCollectionOrder

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

    static NSString *CellIdentifier = @"PhotoCell";
    PhotoCell *cell = (PhotoCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PhotoCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[PhotoCell class]]) {
                cell = (PhotoCell *) currentObject;
                break;
            }
        }
    }

    // Copy the specific dictionary from CellCollection to Cell Individual
    dictCellIndividual = [dictCellCollection objectForKey:[NSString stringWithFormat:@"%@", [arrayCellCollectionOrder objectAtIndex:indexPath.row]]];

    cell.photoCellTitle.text = [dictCellIndividual objectForKey:@"Title"];     // Load cell title
    cell.photoCellImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", [dictCellIndividual objectForKey:@"ThumbnailFilename"]]];        // Load cell image name

    return cell;

}

didSelectRowAtIndexPath不工作。我无法访问arrayCellCollectionOrder

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// Browser
NSMutableArray *arrayPhotos = [[NSMutableArray alloc] init];

    NSLog(@"indexPath.row = %d", indexPath.row);        // Returns the row number i touched, works.

    NSLog(@"arrayCellCollectionOrder = %@", [NSString stringWithFormat:@"%@", [arrayCellCollectionOrder objectAtIndex:indexPath.row]]);        // DOES NOT WORK.

    // Copy the specific dictionary from CellCollection to Cell Individual
    dictCellIndividual = [dictCellCollection objectForKey:[NSString stringWithFormat:@"%@", [arrayCellCollectionOrder objectAtIndex:indexPath.row]]];        // This similar line gives error too.

    ...   ...
    ...   ...
    ...   ...
    ...   ...

}

错误是:      * 由于未捕获的异常'NSRangeException'而终止应用程序,原因:' - [__ NSCFArray objectAtIndex:]:index(1)超出边界(0)'

:我点击了行1,但arrayCellCollectionOrder为NULL。 在arrayCellCollectionOrder中应该有在ViewDidLoad中声明的数据。

我错过了什么吗? 非常感谢。

1 个答案:

答案 0 :(得分:0)

arrayCellCollectionOrder = [[[NSMutableArray alloc] init] retain];
arrayCellCollectionOrder = [temp objectForKey:@"CellCollectionOrder"]; 

你看到你对arrayCellCollectionOrder做了些什么吗?您首先将它分配给新的NSMutableArray(并且不必要地保留它),然后立即孤立数组并将arrayCellCollectionOrder分配给您从temp字典获得的另一个对象。换句话说,第一行没有为你做任何事情,除了创建一个泄漏的可变数组。

如果第二行是正确的并且您获得了一个有效的对象,那就是您想要的,那么问题是我看不到该对象被保留的位置。只要它在字典中,它可能会被保留,但如果temp被丢弃,那么它的成员就会被释放。如果你做了

self.arrayCellCollectionOrder = [temp objectForKey:@"CellCollectionOrder"];

然后设定者会保留它。