1

UITableViewカスタムUITableViewCell(独自のクラスとnib)からにデータをロードしています。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;
}

奇妙なことに、最初の3つのセル(高さ130ピクセル)に読み込まれると機能しますが、下にスクロールしようとするとクラッシュします。(別名、ログにはクラッシュする前に3つの数値が表示されます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:@"#"];

このクラッシュを修正するにはどうすればよいですか?

4

2 に答える 2

3

**私は同じ問題を抱えており、「自己」に取り組んでいます。配列はそれを修正しました。****


NSArrayは、最終的にNSStringとして扱われていた「ObjectAtIndex:#」の単一のインスタンスのみを表していた。NSArrayを作成するときに、tempArrayを作成し、オブジェクトで埋め、リリースされたtempArrayであるMyArrayと等しくなるように設定しました。ただし、「MyArray=tempArray;」と等しく設定します。失敗した。「self.MyArray=tempArray;」で配列OBJECTをアドレス指定するために必要です。100%動作します!!

于 2010-12-19T04:29:36.363 に答える
0

実際のエラーは次のとおりです。

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

objectAtIndex:つまり、NSString( )で実行しようとしましたNSCFStringが、これはもちろんこのメソッドをサポートしていません。

objectAtIndex:次の行で3回呼び出します。

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

したがって、、、またははではなく、postsArrayTitlepostsArrayDateあるように見えます。postsArrayImgNSArrayNSString

于 2010-05-30T10:18:50.273 に答える