NSMutableArray的问题

时间:2011-06-26 18:29:42

标签: iphone objective-c cocoa-touch nsmutablearray

我声明了一个NSMutable数组并为其分配了一些值。

.h
NSMutableArray *imageDetailsFromCategory;

@property (nonatomic, retain) NSMutableArray *imageDetailsFromCategory;

.m
@synthesise imageDetailsFromCategory
ViewDidLoad中的

imageDetailsFromCategory = [[NSMutableArray alloc]init];

//assigning object to Array..working fine.showing two images.
imageDetailsFromCategory = [self  getImageDetailsFromCategory:generatedString];

我有一个问题现在解决了。我有一个问题,即我将此数组传递给 StaticCategoryWithMultiImagePopOver 类。

StaticCategoryWithMultiImagePopOver *staticCategoryWithMultiImagePopOver = [[StaticCategoryWithMultiImagePopOver alloc] init];

[staticCategoryWithMultiImagePopOver setParentImageDetailsArray:imageDetailsFromCategory];

在StaticCategoryWithMultiImagePopOver.h

NSMutableArray *nsmResult;
@property (nonatomic,retain)NSMutableArray *nsmResult;

的.m

@synthesize nsmResult

-(void)setParentImageDetailsArray:(NSMutableArray *)imageDetailsFromCategoryFromParent{
    nsmResult=[[NSMutableArray alloc] init];
    nsmResult=[imageDetailsFromCategoryFromParent retain];

    }

传递的数组在每个索引处保存一个带有一些字符串变量的类对象。

所以我通过代码得到了这个:

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

    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView15 dequeueReusableCellWithIdentifier:CellIdentifier];
    // if (cell == nil) {
    // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    // }
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it


    NSString *documentsDirectory = [paths objectAtIndex:0];

    SymbolTalkEntry *symbolTalkEntry = [[SymbolTalkEntry alloc]init];

    symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];

    NSString *imageNme = symbolTalkEntry.fileName; // *this line*

上述行正在出错。

数组显示计数,但对象超出范围..无法获取值...

任何人都可以告诉我如何才能访问它...我可能知道我的问题是什么......

cellForRowAtIndexPath (工作正常)

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView15 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView15 dequeueReusableCellWithIdentifier:CellIdentifier];
    // if (cell == nil) {
    // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    // }
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it


    NSString *documentsDirectory = [paths objectAtIndex:0];

    SymbolTalkEntry *symbolTalkEntry = [[SymbolTalkEntry alloc]init];

    symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];

    NSString *imageNme = symbolTalkEntry.fileName;

    [symbolTalkEntry release];
    //Display image from app bundle
    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",imageNme]]; 

    cell.imageView.image= [UIImage imageWithContentsOfFile:path]; 
    return cell;


}

-(NSInteger)number

2 个答案:

答案 0 :(得分:3)

这个

nsmResult=[[NSMutableArray alloc] init];
nsmResult=[imageDetailsFromCategoryFromParent retain];

和这个

SymbolTalkEntry *symbolTalkEntry = [[SymbolTalkEntry alloc]init];
symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];

是内存泄漏。

关于你的问题:你得到的对象

symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];

似乎没有属性fileName。

我认为你应该读一本关于Objective-C和Cocoa的好书。

答案 1 :(得分:0)

实际上这是类中的类的问题。

我改变了我的课程:

#import <Foundation/Foundation.h>


@interface SymbolTalkEntry : NSObject {

 int categoryId;
 NSString *runModeType;
 NSString *sceneCategory;
 NSString *fileName;
 int isDefault;
 int sortX;
 int pathId;// 0 for appbundle 1 for document directry


}
@property (nonatomic, retain)NSString *runModeType;;
@property (nonatomic, retain)NSString *sceneCategory;
@property (nonatomic, retain)NSString *fileName;

-(id)init;

-(void) setCategoryId:(int) ctgId;
-(int)categoryId;

-(void) setRunModeType:(NSString *)rmType;
-(NSString *) runModeType;

-(void) setSceneCategory:(NSString *)scCategory;
-(NSString *) sceneCategory;

-(void) setFileName:(NSString *)Flname;
-(NSString *) fileName;

-(void) setIsDefault:(int) isDeft;
-(int)isDefault;

-(void) setSortX:(int) srtX;
-(int)sortX;

-(void) setPathId:(int) srtX;
-(int)pathId;
@end
[5:05:00 AM] Shamsudheen TK: #import "SymbolTalkEntry.h"


@implementation SymbolTalkEntry
@synthesize runModeType;
@synthesize sceneCategory;
@synthesize fileName;


-(id) init{
 categoryId = 0;
 runModeType = @"";
 sceneCategory =@"";
 fileName = @"";
 isDefault = 0;
 sortX =0;
 pathId =0;
 return self;
}
-(void) setCategoryId:(int) ctgId{
 categoryId = ctgId;
}
-(int)categoryId{
 return categoryId;
}

-(void) setRunModeType:(NSString *)rmType{

 if (runModeType != rmType) {  
  [runModeType release ];
  runModeType = [rmType retain];
 }
}
-(NSString *) runModeType{
 return runModeType;
}

-(void) setSceneCategory:(NSString *)scCategory{

 if (sceneCategory != scCategory) {
  [sceneCategory release];
  sceneCategory = [scCategory retain];
 }
}
-(NSString *) sceneCategory{
 return sceneCategory;
}

-(void) setFileName:(NSString *)Flname{

 if (fileName != Flname) {
  [fileName release];
  fileName = [Flname retain];
 }
}
-(NSString *) fileName{
 return fileName;
}

-(void) setIsDefault:(int) isDeft{
 isDefault = isDeft;
}
-(int)isDefault{
 return isDefault;
}

-(void) setSortX:(int) srtX{
 sortX =srtX;
}
-(int)sortX{
 return sortX;
}

-(void) setPathId:(int) srtX{
 pathId = srtX;
}
-(int)pathId{
 return pathId;
}

-(void)dealloc{
 [categoryId release];
 [runModeType release];
 [sceneCategory release];
 [fileName release];
 [isDefault release];
 [sortX release];
 [pathId release];
}
@end

并使用我编写的set方法设置值(例如:[classobj setCategoryId:1])。

现在已超出范围......