1

日付をセクションのタイトルに、行に本の名前を付けて表セクションを表示したいと考えています。NSDictionary を使用してデータをセクションと行にロードする方法がわかりません。したがって、私のコードはすべて台無しです:(サンプルコードをガイドとして、誰か助けてもらえますか?私のNSMutableArrayは次のとおりです:

(
        {
        BIBNo = 187882;
        date = "14/08/2012";
        itemSequence = 000010;
        name = "Increasing confidence / Philippa Davies.";
        noOfRenewal = 0;
        number = 000187907;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    },
        {
        BIBNo = 291054;
        date = "14/08/2012";
        itemSequence = 000010;
        name = "iPhone application development for IOS 4 / Duncan Campbell.";
        noOfRenewal = 2;
        number = 000291054;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    },
        {
        BIBNo = 244441;
        date = "15/08/2012";
        itemSequence = 000010;
        name = "Coach : lessons on the game of life / Michael Lewis.";
        noOfRenewal = 1;
        number = 000244441;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    },
        {
        BIBNo = 290408;
        date = "15/08/2012";
        itemSequence = 000010;
        name = "Sams teach yourself iPhone application development in 24 hours / John Ray.";
        noOfRenewal = 3;
        number = 000290408;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    },
        {
        BIBNo = 161816;
        date = "16/08/2012";
        itemSequence = 000010;
        name = "Malaysian Q & As / compiled by Survey & Interview Department ; illustrations by Exodus.";
        noOfRenewal = 1;
        number = 000161817;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    },
        {
        BIBNo = 187883;
        date = "16/08/2012";
        itemSequence = 000010;
        name = "Positive thinking / Susan Quilliam.";
        noOfRenewal = 3;
        number = 000187899;
        status = "Normal Loan";
        time = "24:00";
        type = Loans;
    }
)

オブジェクトを NSDictionary に実際に再追加して、同じ日付のオブジェクトがまとめて保存され、セクションの行として表示されるようにするにはどうすればよいですか? アドバイスをお願いします!T_T

4

4 に答える 4

2

データを処理するには、次のようなことをする必要があります

NSMutableDictionary *dataSource = [[NSMutableDictionary alloc] init]; // This would need to be an ivar

for (NSDictionary *rawItem in rawItems) {
    NSString *date = [rawItem objectForKey:@"date"]; // Store in the dictionary using the data as the key

    NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date

    if (!section) { // If there is no section then create one and add it to the dataSource
        section = [[NSMutableArray alloc] init];
        [dataSource setObject:section forKey:date];
    }

    [section addObject:rawItem]; // add your object
}

self.dataSource = dataSource;

次に、セクションのタイトルを取得します

NSArray *sections =[[self.dataSource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
return [sections objectAtIndexPath:indexPath.section];

次に、セクション内の行を取得します

NSString *sectionTitle = // get the section title calling the above code
NSArray *items = [self.dataSource objectForKey:sectionTitle];

return [items objectAtIndex:indexPath.row];
于 2012-07-26T17:14:55.043 に答える
0

A-Liveが言ったように、配列を配列の可変辞書にソートする必要があります

次のコードは動作するはずですが、テストされていません

TableView Controller .h で

@interface MyController:UITableViewController{
   NSMutableArray* dict;
   NSMutableArray* array;
}

MyController.m 内

-(void)initWithBooksArray:(NSArray*)bookArray{ //Or something similar

    self = [[super alloc] initWithStyle:UITableViewStylePlain];
    if(self){        
       dict = [[NSMutableDictionary alloc] init];
       array= [[NSMutableArray alloc] init];

       NSMutableArray* sectionArray;
       for(NSDictionary* bookDict in bookArray){

          date = [bookDict valueForKey:@"date"];
          sectionArray = [dict valueForKey:date];
          if(sectionArray){
             [sectionArray addObject:bookDict];
          }else{
             sectionArray = [[NSMutableArray arrayWithObject:bookDict];
             [dict addObject:sectionArray forKey:date];
          }
       }
       array = [[dict allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

    }
    return self;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   return [dict count];
}

-(NSInterger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
   NSString* key = [array objectAtIndex:section];
   return [[dict objectForKey:key]count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
   return [array objectAtIndex:section];
}

...

// I'll leave it to apple's documentation (or someone else) to fill in the blanks on how to create cells

}

于 2012-07-26T17:20:00.537 に答える