【发布时间】: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