cellForRowAtIndexPath:尝试访问数组 objectAtIndex:indexPath.row 时崩溃

发布于 2024-09-03 15:37:38 字数 3316 浏览 5 评论 0原文

我正在从自定义 UITableViewCell (自己的类和笔尖)将数据加载到 UITableView 中。它工作得很好,直到我尝试访问某些数组的 objectAtIndex:indexPath.row

我先发布我的代码,这样你可能会更容易理解我的意思。

- (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;
}

奇怪的是,当它加载到前三个单元格(它们高 130 像素)时它确实可以工作,但是当我尝试向下滚动时它会崩溃。 (也就是说,日志在崩溃之前显示三个数字,0, 1, 2

因此,作为摘要,objectAtIndex:indexPath.row 运行 3*成功 3 次,但是当我尝试在应用程序中向下滚动并加载新单元格时,应用程序崩溃并出现以下错误:

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

有关数组的其他信息:

数组是在 .h< 中创建的/code> file:

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:@"#"];

如何修复此崩溃?

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;
}

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)

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:

The arrays are created in the .h file:

NSArray *postsArrayDate;
NSArray *postsArrayTitle;
NSArray *postsArrayComments;
NSArray *postsArrayImgSrc;

And filled in the 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:@"#"];

How can I fix this crash?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-09-10 15:37:38

** 我有同样的问题,解决“自我”。数组修复了它。 **


The NSArray was only representing the singular instance of the "ObjectAtIndex:#" which ultimately was being treated as a NSString. When creating the NSArray, I created tempArray, filled it with objects, set it equal to MyArray, the released tempArray. But setting it equal to "MyArray = tempArray;" failed. Required to address the array OBJECT with "self.MyArray = tempArray;" which works 100% !!

** I had the same issue, addressing the "self." array fixed it. **


The NSArray was only representing the singular instance of the "ObjectAtIndex:#" which ultimately was being treated as a NSString. When creating the NSArray, I created tempArray, filled it with objects, set it equal to MyArray, the released tempArray. But setting it equal to "MyArray = tempArray;" failed. Required to address the array OBJECT with "self.MyArray = tempArray;" which works 100% !!

转身泪倾城 2024-09-10 15:37:38

实际的错误是这样的:

-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5d10c20

换句话说,您尝试在 NSString (NSCFString) 上运行 objectAtIndex:,这当然不支持这个方法。

您在以下几行中调用 objectAtIndex: 三次:

cell.titleLabel.text = [postsArrayTitle objectAtIndex:indexPath.row];
cell.dateLabel.text = [postsArrayDate objectAtIndex:indexPath.row];
cell.cellImage.image = [UIImage imageWithContentsOfFile:[postsArrayImg objectAtIndex:indexPath.row]];

因此,postsArrayTitlepostsArrayDatepostsArrayImg 似乎是不是一个 NSArray,而是一个 NSString

The actual error is this:

-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5d10c20

In other words, you tried to run objectAtIndex: on an NSString (NSCFString), which of course does not support this method.

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]];

So it appears that postsArrayTitle, postsArrayDate, or postsArrayImg isn't an NSArray, but an NSString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文