1

テーブルセルに xml リストからタイトルとイメージ URL を入力したいと考えています。タイトル (NSMutableDictonary *sections) と imageURL (NSMutableDictonary *sectionsImg) をそれぞれ 2 つの NSMutableDictionary に保存することができました。

/*******This is in viewDidLoad***/
Directory *allDirectory = [appDelegate.directories objectAtIndex:0];

for (allDirectory in appDelegate.directories)
{
    NSDictionary *dica = [NSDictionary dictionaryWithObject:allDirectory.dirTitle forKey:@"dirTitle"];
    NSDictionary *dico = [NSDictionary dictionaryWithObject:allDirectory.imageURL forKey:@"imageURL"];


    [dirName addObject:dica];
    [dirImage addObject:dico];
    //NSLog(@"dic of items : %@",dirImage);


}
for (allDirectory in appDelegate.directories)
{
    //retrieve the first letter from every directory title (dirTitle)
    NSString * c = [allDirectory.dirTitle substringToIndex:3];

    NSString * m = allDirectory.imageURL;



    found = NO;
    find = NO;

    for (NSString *str in [self.sections allKeys])
    {
        if ([str isEqualToString:c])
        {
            found = YES;
        }
    }
    for (NSString *stra in [self.sectionsImg allKeys])
    {
        if([stra isEqualToString:m])
        {
            find = YES;
        }
    }

    if (!found)
    {
            [self.sections setValue:[[NSMutableArray alloc]init] forKey:c ];
            [self.sectionsImg setValue:[[NSMutableArray alloc]init] forKey:m];
    }
    if (!find)
    {
        [self.sectionsImg setValue:[[NSMutableArray alloc]init] forKey:m];
    }

}

for (NSDictionary *directory in dirName)
{
    [[self.sections objectForKey:[[directory objectForKey:@"dirTitle"] substringToIndex:3]] addObject:directory];
    //NSLog(@"hehehe have : %@",sections);
}
for (NSDictionary *directoryImg in dirImage)
{
    //[[self.sectionsImg objectForKey:[[directoryImg objectForKey:@"imageURL"] substringFromIndex:0]] addObject:directoryImg];
    [[self.sectionsImg objectForKey:[directoryImg objectForKey:@"imageURL"]] addObject:directoryImg];

    //NSLog(@"HOHOHO have : %@",sectionsImg);
}

そして cellForRowAtIndexPath で辞書を宣言します

NSDictionary *dictionary = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:@"dirTitle"];

しかし、imageURLの辞書を宣言しようとしたとき

NSDictionary *dictionaryImg = [[self.sectionsImg valueForKey:[[[self.sectionsImg allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

それは私にエラーを与えます: キャッチされていない例外 'NSRangeException' によるアプリの終了、理由: ' * -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

理由はありますか?XMLのタイトルとURLを取得して表示できるロジックは同じであると想定されています。タイトルは取得できますが、imageURL は取得できません。助けていただければ幸いです。

4

1 に答える 1

4

配列をソートしようとしています...配列が配列ではなくNSDictionaryであるという事実を除いて。

あなたのコードは現時点では最適ではありません。あなたは辞書の考え方を間違っており、それらを配列と混同している可能性があります.

私が間違っていなければ、2 つのリストがあります。最初のリストは名前のリストで、2 番目のリストはその名前に対応するイメージです。

以下、2つのことを行います。

  • まず、問題を解決する 2 つの方法を紹介します。サンプルコードが含まれており、簡単な説明が含まれています。私が説明したことの一部を理解していない可能性があります。その場合、次のことを行う必要があります。
  • 2 つのソリューションの下で説明したリンクを確認してください。配列、辞書、テーブル、そしてボーナスとして XML 解析に関するすべてを理解できるようにするチュートリアルがあります。

したがって、私の意見では、次の 2 つのことを行うことができます。


最初のものは NSDictionaries の配列を使用しています。次のようなコードを使用します。

NSMutableDictionary *itemOne = [[NSMutableDictionary alloc] init];
NSMutableDictionary *itemTwo = [[NSMutableDictionary alloc] init];
NSMutableArray *listOfAll = [[NSmutableArray alloc] init];
NSString *itemOneName = [[NSString alloc] initWithFormat:@"This is picture 1"];
NSString *itemTwoName = [[NSString alloc] initWithFormat:@"This is picture 2"];
NSData *imageOneData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic1.jpg"]];
NSData *imageTwoData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic2.jpg"]];
UIImage *itemOneImage = [UIImage imageWithData: imageOneData];
UIImage *itemTwoImage = [UIImage imageWithData: imageTwoData];
[itemOne setObject:itemOneNameString forKey:@"Name"];
[itemOne setObject:itemOneImage forKey:@"Image"];
[itemTwo setObject:itemTwoNameString forKey:@"Name"];
[itemTwo setObject:itemTwoImage forKey:@"Image"];
[listOfAll addObject:itemOne];
[listOfAll addObject:itemTwo];

その配列を使用して、何でも埋めることができます。forループで何かを使用して、配列を反復処理するだけです。

for (int i = 0; i < [listOfAll count]; i++)
{
    NSMutableDictionary *currentItem = [[NSMutableDictionary alloc] initWithDictionary:[listOfAll objectAtIndex:i]];
    //Do something with that current item
}

tableView でそのインデックスを使用することもできます。その場合、目的のインデックスを取得する section代わりに、変数を使用する必要があります。i


2 つ目は、2 つの配列を使用しています。imageOneテキストで名前が付けられた画像を取得すると想像してくださいimageName。次に、次を使用する必要があります。

NSMutableArray *nameList = [[NSMutableArray alloc] init];
[nameList addObject: imageName];
NSMutableArray *imageList = [[NSMutableArray alloc] init];
[imageList addObject: imageOne];

これらのリストから特定のアイテムを使用したい場合は、同じインデックス番号を使用するだけです。例えば:

[theTitleLabel setText:[[NSString alloc] initWithFormat:@"%@", [nameList objectAtIndex:x]]];
[theImageView setImage:[imageList objectAtIndex:x]];

xが同じ数であることを確認してください。


特にObjective-Cを初めて使用する場合は、これがすべて多くの情報であることを理解しています。配列、辞書、およびテーブルビューの使用方法に関する多くの情報を提供するチュートリアルが存在します。おまけとして、XML 構文解析について少し知ることができます。 そのチュートリアルを見て、すべてを実行し、そこに書かれていることをすべて読むことをお勧めします. これにより、iPhone でのプログラミングの世界への良いスタートが切れるはずです。

幸運を!

于 2011-05-10T09:20:41.170 に答える