尝试使用 UIImage: imageNamed: 设置 cell.image 感到困惑:
我创建了一个包含 NSDictionary 结构的 plist。 键是一个标签(我用它来填充 tableview cell.text),值是一个文件名,我想用它来“动态”填充 cell.image。 确切的 png 文件名将加载到项目中。
为了让事情变得更简单(我是这么想的),我将 plist 加载到 NSDictionary 中,然后将其分成两个数组(使用 allKeys 和 allValues)。
在尝试设置单元格图像时,我不明白为什么它有效:
cell.image = [UIImage imageNamed:@"bad.png"];
并且这不起作用(在模拟器中 - 我在调试器中遇到应用程序崩溃和呕吐):
cell.image = [UIImage imageNamed:[rowImageList objectAtIndex:indexPath.row]];
我可以将下一行放在第二段代码之前并且它可以工作很好,看来我从 objectAtIndex 获取了一个 NSString:
NSLog(@"Image name: %@", [rowImageList objectAtIndex:indexPath.row]);
我还尝试创建一个临时 NSString 并为其分配 [rowImageList objectAtIndex:],然后使用该 NSString 来提供 imageNamed:。
以下是来自调试器的一些信息:
-[NSCFArray length]:无法识别的选择器发送到实例 0x262d910
由于未捕获而终止应用程序 例外 'NSInvalidArgumentException',原因: '*** -[NSCFArray 长度]:无法识别 选择器发送到实例 0x262d910'
如果您需要更多信息/代码,请告诉我。
编辑:这是我填充 rowImageList 的方法:
NSArray *myRowImages = [[NSArray alloc] init];
myRowImages = [searchFilterList allValues];
self.rowImageList = [myRowImages copy];
[myRowImages release];
//where searchFilterList is an NSDictionary that I loaded from a plist
这是我的 plist 的屏幕截图: [IMG][]1 [/IMG]
编辑:修改 rowImageList 人口:
NSMutableArray *myRowImages = [[NSMutableArray alloc] init];
NSEnumerator *enumerator = [searchFilterList objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
[myRowImages addObject:[object objectAtIndex:0]];
}
self.rowImageList = myRowImages; //edit: added self. prefix
[myRowImages release];
I created a plist that contains an NSDictionary structure. The key is a label (which I use to populate tableview cell.text) and the value is a file name that I want to use to 'dynamically' populate cell.image. The exact png filenames are loaded into the project.
To make things easier (so I thought), I load the plist into an NSDictionary, then pull that apart into two arrays (using allKeys and allValues).
In attempting to set a cell image, I dont understand why this works:
cell.image = [UIImage imageNamed:@"bad.png"];
and this does not (in simulator - I get an app crash and barfing in debugger):
cell.image = [UIImage imageNamed:[rowImageList objectAtIndex:indexPath.row]];
I can put this next line right before the second piece of code and it works just fine, so it appears that I am getting an NSString from objectAtIndex:
NSLog(@"Image name: %@", [rowImageList objectAtIndex:indexPath.row]);
I have also tried creating a temp NSString and assigning [rowImageList objectAtIndex:] to it, then using that NSString to supply imageNamed:.
Here is some info from debugger:
-[NSCFArray length]: unrecognized selector sent to instance 0x262d910
Terminating app due to uncaught
exception
'NSInvalidArgumentException', reason:
'*** -[NSCFArray length]: unrecognized
selector sent to instance 0x262d910'
Let me know if you need more info/code.
Edit: Here's how I populate rowImageList:
NSArray *myRowImages = [[NSArray alloc] init];
myRowImages = [searchFilterList allValues];
self.rowImageList = [myRowImages copy];
[myRowImages release];
//where searchFilterList is an NSDictionary that I loaded from a plist
Here's a screenshot of my plist:
[IMG][]1
[/IMG]
Edit: revised rowImageList population:
NSMutableArray *myRowImages = [[NSMutableArray alloc] init];
NSEnumerator *enumerator = [searchFilterList objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
[myRowImages addObject:[object objectAtIndex:0]];
}
self.rowImageList = myRowImages; //edit: added self. prefix
[myRowImages release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会仔细检查 NSLog() 的输出,并确保它不是包含 NSString 的数组。 这就是错误告诉你的; 您正在传递一个 NSArray ,其中需要一个 NSString 。
I would double-check the output from NSLog() and make sure it's not an array that contains an NSString. That's what the error is telling you; you're passing an NSArray where an NSString is expected.
此错误意味着 [rowImageList objectAtIndex:indexPath.row] 返回一个数组,而不是预期的字符串。
之所以打印一个值,是因为打印一个数组还需要在NSLog语句中指定%@。
尝试调试该方法并检查 [rowImageList objectAtIndex:indexPath.row] 的类。 它很可能是一个数组。
This error means that [rowImageList objectAtIndex:indexPath.row] is returning an array and not a string as expected.
The reason why a value is printed is because to print an array also you need to specify %@ in the NSLog statement.
Try debugging the method and check the class of [rowImageList objectAtIndex:indexPath.row]. It will most probably turn out to be an array.
您确定您的 plist 是键/字符串对,并且您没有以某种方式将名称包装在数组中吗?
Are you sure your plist is key/string pairs, and you have not somehow wrapped the names in an array?