问题描述
|
构建表格视图单元格时,我试图将字符串的值从数组传递给实例“ 0”,但是我得到的值为nil。这是代码:
Nsstring *file = [[Nsstring alloc] init ];
file = [photoFiles objectAtIndex:indexPath.row];
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@\"ImageViewer\" bundle:nil];
imageView.fileName = file;
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
您能帮我解决这个问题吗?
解决方法
如果
imageView.fileName = file
设置为nil
值,则您可能应该考虑分析photoFiles
数组的内容。
该数组可能在索引“ 5”处没有值。您应该通过调试器或日志打印来检查:
NSLog(@\"Array contents: %@\",[photoFiles description]);
编辑
您可以以更简洁的方式编写此代码。即:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@\"ImageViewer\" bundle:nil];
imageView.fileName = [photoFiles objectAtIndex:indexPath.row];
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
另外,您应避免指向nil
NSBundle
。即将第一行更改为:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@\"ImageViewer\" bundle:[NSBundle mainBundle]];
但它也应该以这种方式工作,因为类名和Nib名称是相同的:
ImageViewer *imageView = [[ImageViewer alloc] init];
试试看,让我知道是否有所改变。
,检查线
file = [photoFiles objectAtIndex:indexPath.row];
和
检查imageviewer中文件名的属性
,首先,您不必为NSString分配内存。其次,您是否在ImageViewer中设置了fileName的属性?
,好吧,这里没有足够的信息来给出答案(您需要使用调试器逐步了解它,并弄清楚为什么返回零值),并且至少有一个内存泄漏。这是我重写您提供的代码的方式:
ImageViewer *imageView = [[ImageViewer alloc] initWithNibName:@\"ImageViewer\" bundle:nil];
imageView.fileName = [photoFiles objectAtIndex:indexPath.row];
[self.navigationController pushViewController:imageView animated:YES];
[imageView release];
,我有办法做到...可能会帮助您...
我在表视图中有NSarray ...下一个视图是NsDictionary
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int actualRowIndex = [[filteredIndexs objectAtIndex:indexPath.row] intValue];
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
selectedMeal *selctedMealView = [[selectedMeal alloc] initWithNibName:@\"selectedMeal\" bundle:nil];
selctedMealView.offerDetails = [offers objectAtIndex:actualRowIndex];
[self.navigationController pushViewController:selctedMealView animated:YES];
//[selctedMealView changeText:[arryList objectAtIndex:indexPath.row]];
[selctedMealView release];
}
在这里,selectedmeal是我的下一个视图,offers是我在表视图中的NSArray,offerDetails是我在所选餐点视图中的NSDictionary
也在表视图中我创建了NSdictionary并设置了这样的对象
NSString *o_resturant_name = [offer objectForKey:@\"restaurant_name\"];
[[Globals sharedGlobals].resturantList addObject:o_resturant_name];
和viewdidload中的selectedmeal视图我读取了该值...
NSString *resturantNameText = [offerDetails objectForKey:@\"restaurant_name\"];
这对我来说是完美的...
也许这会帮助你