手動で作成されたセクションでコアデータテーブルビューの行をグループ化します

StackOverflow https://stackoverflow.com/questions/4775080

  •  23-10-2019
  •  | 
  •  

質問

私は持っています Company 約20のフィールドを持つエンティティで、手動で作成されたセクションヘッダーを備えたグループ化されたテーブルビューを使用したいと考えています(つまり、一般、金融、その他)が、コアデータにこれらのセクションヘッダーを処理して作成する方法を理解する方法がわかりません。それらは、これらのグループに表示したいデータのみに関連しています。

たとえば、名前、ロゴなどは一般的になり、予算、現金は財政的になります。

基本的に、コアデータからどのデータが各カテゴリに配置されるかを制御して表示したいと思います。

コアブックのサンプルには、このコードがあります。

/*
 The data source methods are handled primarily by the fetch results controller
 */

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

しかし、セクションがコアデータに含まれていないが、手動で作成されたことをどのように理解させることができますか?

役に立ちましたか?

解決

私は今私の問題に対する答えを持っています、私はそれが正しいアプローチかどうかはわかりませんが、それは機能しており、コメントを歓迎します。

問題が何であるか、そして私が何をしようとしていたかを明確にするためだけです。

コアデータエンティティがあります company ただし、約10匹ほどのフィールドがありますが、それらをすべて一度にリストするのではなく、出力フィールドをグループ化したかったのです。

たとえば、「現金」、「MarketingBudget」、「SeoBudget」などの現金に関連する約6つのフィールドがあり、このデータをTableViewにグループ化したかったのですが、問題はセットアップ方法がわからなかったことでした。 table.field.xがgroup.xなどに属しているための関係。

私が来た答えは、コアデータエンティティの構造をほとんど反映したプリスト/辞書を使用することでした。表示したいグループに構造を割り当てます。

私の辞書はこのように見えます。

(根)

- > companytpl(array)

- >アイテム0(辞書)

--->セクション(string)= "general"

--->子供(配列)------>アイテム0(辞書)

----------> key = "name"

----------> value = "会社名" ...

どこ Key 必要に応じて、コンテンツを使用および表示するコアデータのリファレンスになります。

どこ Value CellForRowAtIndexPathに表示されるでしょう。

したがって、私のコードでは、基本的にセクション(TableViewセクションを意味します)を調べてから、Plistから相関する子供の情報を見つけました。キー/値を取得し、必要に応じてこれを使用します。

これがコードのカットダウンバージョンです。

- (void)viewDidLoad {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"CompanyTpl" ofType:@"plist"];
    self.companyDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] retain];

    // self.tableDataSource is a NSMutableArray
    self.tableDataSource = [self.companyDictionary objectForKey:@"CompanyTpl"];

    // Debugging info
    NSLog(@"Section = 0");

    NSLog(@"%@", [self.tableDataSource objectAtIndex:0]);

    NSLog(@"Section Name = %@", [[self.tableDataSource objectAtIndex:0] objectForKey:@"Section"]);


    NSArray *sectionChildren = [[self.tableDataSource objectAtIndex:0] objectForKey:@"Data"];

    NSLog(@"Section Children = %@", sectionChildren);
    NSLog(@"Count of Section Children = %d", [sectionChildren count]);


}

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

// Section header
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;

    title = [[self.tableDataSource objectAtIndex:section] objectForKey:@"Section"];

    return title;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
NSInteger rows = 0;

    NSArray *sectionChildren = [[self.tableDataSource objectAtIndex:section] objectForKey:@"Data"];

    NSLog(@"Section Children = %@", sectionChildren);
    NSLog(@"Count of Section Children = %d", [sectionChildren count]);

    rows = [sectionChildren count];


    return rows;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    NSArray *sectionChildren            = [[self.tableDataSource objectAtIndex:indexPath.section] objectForKey:@"Data"];
    NSDictionary *sectionChildrenData   = [sectionChildren objectAtIndex:indexPath.row];

    //NSLog(@"Section Children data = %@", sectionChildrenData);

    NSString *scKey     = [sectionChildrenData objectForKey:@"Key"];
    NSString *scValue   = [sectionChildrenData objectForKey:@"Value"];

    NSLog(@"scKey = %@", scKey);

    // Grab the data from Core Data using the scKey



    static NSString *CellIdentifier = @"defaultCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];


        //cell.textLabel.text = @"test";

        cell.textLabel.text = scValue;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    return cell;
}

アイデアは、コアデータを参照してコンテンツをつかみ、TableViowコントローラーにcelloRowAtindexpath cell.textlabel.text値に表示するときにキーを使用できるということです。

もう少し詳細に進み、字幕がどうあるべきかなど、整理に詳細を確認することができます。

とにかく、コメントや考えを歓迎します。

ありがとう。

他のヒント

私は答えに少し近づいていますが、特定のセクションにあるようにコアデータ項目を接続するのに苦労しています。

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //return [[fetchedResultsController sections] count];

    //NSLog(@"%d", [self.sectionArray count] );

    return 4;
}

// Section header
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;

    switch (section) {
        case 0:
            title = @"General";
            break;
        case 1:
            title = @"Finanical";
            break;
        case 2:
            title = @"Category A";
            break;
        case 3:
            title = @"Category B";
            break;
        case 4:
            title = @"Misc";
            break;
        default:
            break;
    }

    return title;
}



// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    /*
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
     */

    NSInteger rows = 0;

    // The number of rows depend on the section
    switch (section) {
        case 0:
            rows = 2;
            break;
        case 1:
            rows = 3;
            break;
        case 2:
            rows = 4;
            break;
        default:
            break;
    }

    return rows;
}

これが行うことは、4つのセクションを手動で作成することです。現時点では名前は重要ではありません。その後、各セクションの異なる数の行を作成します。

ここまでは順調ですね。

私が抱えている問題は、CESTION0.ROW0でTextLabelに会社名などを言ってほしいことをコアデータに理解させることです。

これはすべて辞書にあり、私が望む構造全体と表示するラベルをレイアウトする必要があると思います。そして、cellforrowatindexpathで、私が望む辞書内に配列を表示します。

つまり:

root] companytpl(配列)

- >アイテム0(辞書)

-----> category(string)= "general"

----->データ(配列)

--------->アイテム0(辞書)

--------------> cdfieldName:名前

-------------->ディスプレイ:「会社名」

CDFieldNameは、表示したいコアデータ内のフィールド名への参照です。

これを行う別の方法がある場合、私は知ることに興味があります。

ありがとう。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top