问题描述
我正在从自定义UITableViewCell
(自己的类和笔尖)将数据加载到UITableView
.效果很好,直到我尝试访问某些数组的objectAtIndex:indexPath.row
.
I am loading in data to a UITableView
, from a custom UITableViewCell
(own class and nib). It works great, until I try to access the objectAtIndex:indexPath.row
for some arrays.
我将首先发布我的代码,这可能会让您更容易理解我的意思.
I'll post my code first, it will probably be easier for you to understand what I mean then.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[CustomCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
// Configure the cell...
NSUInteger row = indexPath.row;
cell.titleLabel.text = [postsArrayTitle objectAtIndex:indexPath.row];
cell.dateLabel.text = [postsArrayDate objectAtIndex:indexPath.row];
cell.cellImage.image = [UIImage imageWithContentsOfFile:[postsArrayImgSrc objectAtIndex:indexPath.row]];
return cell;
}
奇怪的是,它在加载前三个单元格(它们的高度为130px)时确实起作用,但是当我尝试向下滚动时会崩溃. (又是,日志在崩溃前显示了三个数字,0, 1, 2
)
The odd thing is, it does work when it loads in the first three cells (they are 130px high), but crashes when I try to scroll down. (Aka, the log shows three numbers before it crashes, 0, 1, 2
)
因此,作为总结,objectAtIndex:indexPath.row
成功运行了3次* 3次,但是当我尝试在应用程序中向下滚动并加载新的单元格时,应用程序崩溃并出现以下错误:
So, as a summary, the objectAtIndex:indexPath.row
runs 3*3 times successfully, but when I try to scroll down in the app, loading in new cells, the app crashes with the following error:
2010-05-30 14:00:43.122 app[2582:207] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5a44bf0
2010-05-30 14:00:43.124 app[2582:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5a44bf0'
// Stack
0 CoreFoundation 0x02398c99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x024e65de objc_exception_throw + 47
2 CoreFoundation 0x0239a7ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0230a496 ___forwarding___ + 966
4 CoreFoundation 0x0230a052 _CF_forwarding_prep_0 + 50
5 myappname 0x00002ab1 -[HomeTableViewController tableView:cellForRowAtIndexPath:] + 605
有关数组的其他信息:
Additional information on the arrays:
在.h
文件中创建数组:
NSArray *postsArrayDate;
NSArray *postsArrayTitle;
NSArray *postsArrayComments;
NSArray *postsArrayImgSrc;
并填写viewDidLoad:
:
NSURL *urlPosts = [NSURL URLWithString:@"http://mysite/myphpfile.php?posts=2"]; //returns data in this format: DATA1#Data1.1#data1.2~DATA2#data2.1#~ and so on.
NSError *lookupError = nil;
NSString *data = [[NSString alloc] initWithContentsOfURL:urlPosts encoding:NSUTF8StringEncoding error:&lookupError];
postsData = [data componentsSeparatedByString:@"~"];
[data release], data = nil;
urlPosts = nil;
postsArrayDate = [[postsData objectAtIndex:2] componentsSeparatedByString:@"#"];
postsArrayTitle = [[postsData objectAtIndex:3] componentsSeparatedByString:@"#"];
postsArrayComments = [[postsData objectAtIndex:4] componentsSeparatedByString:@"#"];
postsArrayImgSrc = [[postsData objectAtIndex:5] componentsSeparatedByString:@"#"];
如何解决此崩溃?
推荐答案
实际错误是这样的:
-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5d10c20
换句话说,您尝试在NSString
(NSCFString
)上运行objectAtIndex:
,它当然不支持此方法.
In other words, you tried to run objectAtIndex:
on an NSString
(NSCFString
), which of course does not support this method.
在以下几行中,您多次呼叫objectAtIndex:
:
You call objectAtIndex:
three times, in these lines:
cell.titleLabel.text = [postsArrayTitle objectAtIndex:indexPath.row];
cell.dateLabel.text = [postsArrayDate objectAtIndex:indexPath.row];
cell.cellImage.image = [UIImage imageWithContentsOfFile:[postsArrayImg objectAtIndex:indexPath.row]];
因此,看来postsArrayTitle
,postsArrayDate
或postsArrayImg
不是NSArray
,而是NSString
.
So it appears that postsArrayTitle
, postsArrayDate
, or postsArrayImg
isn't an NSArray
, but an NSString
.
这篇关于cellForRowAtIndexPath:尝试访问数组objectAtIndex:indexPath.row时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!