0

私が欲しいもの:テーブルビュー内にデータのリストを表示します。

データは、異なるオブジェクトを作成することにより、単一の配列内に格納されます。したがって、オブジェクトごとに、配列のサイズは異なります。だから私の問題は、データを表示しているときに、テーブルのセルを完全に埋めるのではなく、一部のセルが空のままになっていることです。見栄えがよくありません。

空のセルを追加せずにテーブル ビューを取得する方法を説明できる人はいますか?

データを整理する方法は次のとおりです。

Recipe *recipe1 = [Recipe new];


recipe1.ingredients = [NSArray arrayWithObjects:@"1. Fish  - 500 g .",@"2. Onion (Big) - 2 nos (Chopped)",@"3. Garlic (Chopped) - 3 flakes",@"4. Green chillies - 2 nos (Chopped)",@"5. Chilly powder - 3/4 tbsp  ",@"6. Coriander powder - 1/2 tsp ",nil];


Recipe *recipe2 = [Recipe new];

recipe2.ingredients = [NSArray arrayWithObjects:@"1. fish - 300 gm",@"2. Tomato(medium) - 1 no",@"3. Garlic - 10 gm",@"4. Coconut powder - 10 gm",@"5. Curry leaves - A few",@"6. Salt - As reqd",@"7. Onions(medium) - 2 nos(chopped)",@"8. Oil - 1 - 2 tbsp",nil];

選択した料理に応じて、これらの食材リストをテーブルビュー内に表示したいと考えています。

これが私のデータソースメソッドです

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [data count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    [cell.textLabel setFont:[UIFont fontWithName:@"Bold" size:20]];

    // Configure the cell.
    cell.textLabel.text =[data objectAtIndex:indexPath.row];
    return cell;
}
4

4 に答える 4

0

viewDidLoad で 1 行追加

tableview.tableFooterView = [[UIView alloc] init];

また、これを下のメソッドに追加します。したがって、空の追加セルは表示されません

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
 }
于 2015-10-20T09:28:56.197 に答える
0

次の関数を変更します: for (MAC OS)

 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
    {
         if (First Recipe)
    return [recipe1.ingredients count];

       else if (Second Recipe)
    return [recipe2.ingredients count];

    }

または iOS の場合:

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       if (First Recipe)
            return [recipe1.ingredients count];

               else if (Second Recipe)
            return [recipe2.ingredients count];
    }

その後、選択が完了したら、必ずテーブル ビューをリロードしてください。これがあなたを助けますように。

于 2013-03-12T05:44:44.017 に答える
0

あなたがしたいことは:

レシピオブジェクトでいっぱいの配列があり、それがテーブルビューのデータソースになります。&レシピ(名前)のいずれかをクリックすると、別のView Controllerにプッシュされ、そのレシピに必要な材料のリストがテーブルビュー(リストとして)で提供されます。

このためには、まず、recipeArray (レシピ オブジェクト) をデータソースとして使用してメイン ビュー コントローラーを作成し、レシピ名を使用してテーブル ビューにレシピを一覧表示します。レシピを選択すると( In didSelectRowAtIndexPath が呼び出されます)、選択したレシピオブジェクトを使用して新しいビューコントローラーにプッシュし、次のテーブルビューコントローラーに成分リストを入力します。

于 2013-03-12T05:59:40.300 に答える