【问题标题】:problem with NSMutableArrayNSMutableArray 的问题
【发布时间】:2011-06-26 18:29:42
【问题描述】:

我声明了一个 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

【问题讨论】:

  • 如果你以后要指向另一个对象,为什么总是alloc init
  • 对不起,这段代码似乎完全错误。我建议一本关于 Objective-C 的好书。
  • 您显然会从您对[nsmResult objectAtIndex:indexPath.row] 的调用中返回一个 NSArray,而不是 SymbolTalkEntry(或者 SymbolTalkEntry 可能是 NSArray 的子类)在任何一种情况下,您都应该从编译器收到大量警告这段代码,阅读它们。
  • 等等。什么?您正在为显示的每个单元格点击文件系统?这似乎会对性能产生巨大影响。
  • 在 Xcode 中编写代码并不相关,而您应该标记它为 cocoa-touch,因为您使用的是框架接口和类。此标记比标记 iphone 或 ipad 更准确​​,因为它直接引用您正在使用的技术而不是设备,可以与其他技术一起使用,例如 webapps 或非苹果框架,例如 monoTouch。

标签: iphone objective-c cocoa-touch nsmutablearray


【解决方案1】:

这个

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

还有这个

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

是内存泄漏。

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

symbolTalkEntry =[nsmResult objectAtIndex:indexPath.row];

似乎没有属性文件名。

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

【讨论】:

    【解决方案2】:

    其实是数组里面的类的问题。

    我改变了我的班级:

    #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])。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多