Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa touch NSMUTABLEARRY在ViewDidLoad中工作,但在DidSelectRowAtIndexPath中不工作_Cocoa Touch_Nsmutablearray - Fatal编程技术网

Cocoa touch NSMUTABLEARRY在ViewDidLoad中工作,但在DidSelectRowAtIndexPath中不工作

Cocoa touch NSMUTABLEARRY在ViewDidLoad中工作,但在DidSelectRowAtIndexPath中不工作,cocoa-touch,nsmutablearray,Cocoa Touch,Nsmutablearray,菜单 菜单m ViewDidLoad正常工作 @interface Menu : UITableViewController { NSMutableArray *arrayCellCollectionOrder; NSMutableDictionary *dictCellCollection; NSMutableDictionary *dictCellIndividual; } @property (nonatomic, retain) NSMutableArr

菜单

菜单m

ViewDidLoad正常工作

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

@property (nonatomic, retain) NSMutableArray *arrayCellCollectionOrder;

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

@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.

}
- (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;

}
未选择行索引路径不工作。我无法访问
arrayCellCollectionOrder

@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.

}
- (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;

}
错误是: *由于未捕获的异常“NSRangeException”而终止应用程序,原因:“-[\u NSCFArray objectAtIndex:]:索引(1)超出边界(0)”

i、 e:我单击了行
1
,但
arrayCellCollectionOrder
为空。
arrayCellCollectionOrder
中应该有ViewDidLoad中声明的数据

有什么我错过的吗? 先谢谢你

- (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.

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

}
您是否看到正在对
arrayCellCollectionOrder
执行的操作?首先将其分配给一个新的NSMutableArray(并且不必要地保留它),然后立即孤立该数组,并将
arrayCellCollectionOrder
分配给从
temp
字典中获取的另一个对象。换句话说,第一行除了创建一个泄漏的可变数组之外,对您没有任何帮助

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

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

然后二传手会保留它。

谢谢!我发现了我的错误。。改成下面这行也行。arrayCellCollectionOrder=[[NSMutableArray alloc]init];arrayCellCollectionOrder=[[temp objectForKey:@“CellCollectionOrder”]保留];如果你那样做的话,你就是在泄密。你为什么要做alloc和init呢?其次,您已经定义了一个@property,并且设置了setter和getter(@synthesis),那么当您可以使用setter时,为什么还要进行手动保留呢?执行我的建议:1)删除arrayCellCollectionOrder=[[NSMutableArray alloc]init]retain];2) 将另一行更改为:self.arrayCellCollectionOrder=[temp objectForKey:@“CellCollectionOrder]”;内存管理对我来说是新事物。我想你是对的
self.arrayCellCollectionOrder
是正确的方法。谢谢。试着去理解你为什么要做出这些改变,我想这会在将来避免头痛。如果将一个对象分配给一个变量,并将另一个对象分配给同一个变量,那么第一个对象很可能已成为孤立对象,并且可能已泄漏。此外,您不必使用setter和getter,因此,如果您像将要做的那样手动执行保留,则不需要定义属性或合成。但是property和Synthesis允许您做的是允许其他类访问您的类的实例变量。(我过于简单化了)