【问题标题】:cellForRowAtIndexPath: crashes when trying to access arrays objectAtIndex:indexPath.rowcellForRowAtIndexPath:尝试访问数组 objectAtIndex:indexPath.row 时崩溃
【发布时间】:2010-05-30 09:38:56
【问题描述】:

我正在将数据从自定义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 文件中创建:

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

我该如何解决这个崩溃?

【问题讨论】:

  • 如果有人想向我解释为什么在填充数组时在 viewDidLoad 中使用 self.array 来修复它,我会接受这个答案。谢谢。
  • 还有什么自我。部分在其他比较中。
  • 对不起,但我一点也不知道为什么将self. 添加到postsArrayDate 等解决了它...
  • @Douwe M:提示:保留对象... :)
  • 遇到了同样的问题,谁能详细说明为什么 self.有什么不同吗?我在这里有点困惑。

标签: iphone uitableview nsindexpath


【解决方案1】:

** 我有同样的问题,解决“自我”。阵列修复它。 **


NSArray 仅代表“ObjectAtIndex:#”的单个实例,最终被视为 NSString。在创建 NSArray 时,我创建了 tempArray,用对象填充它,将其设置为等于 MyArray,即释放的 tempArray。但将其设置为“MyArray = tempArray;”失败的。需要使用“self.MyArray = tempArray;”来寻址数组 OBJECT; 100%有效!

【讨论】:

    【解决方案2】:

    实际的错误是这样的:

    -[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

    【讨论】:

    • 不,不可能,当我尝试 NSLog 时它也崩溃了。
    • EDIT 我尝试使用 %@ 而不是 %d 进行 NSLog。但我知道那些数组包含字符串,但它们是数组。
    • 而且,不可能是这样,因为正如我所写,前三个单元格(向下滚动之前)从这些数组中完美加载。
    • 嗯,可能还有其他问题,但这绝对是发生该错误的原因。
    • 是的,这行得通。 NSLog(@"%d", indexPath.row); 也可以,如果我把它放在cellForRowAtIndexPath: 的绝对开头,它也会在崩溃之前打印数字 3。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    相关资源
    最近更新 更多