简体   繁体   中英

Tableview with empty cells

What I Want: Display some list of data inside a table view.

The data is stored inside a single array by creating different objects .So for each object ,Size of the array varies. So my problem is while showing the data,Instead of filling the table cells completely,some cells are left empty. It is not looking good.

Can anyone explain how to get a table view without any extra empty cells?

Here is how i arrange data:

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];

I want to show these ingredients list inside tableview depending upon which dish is selected.

Here is my data source methods

- (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;
}

Inside cellForRowAtIndexPath method try using following

On second Recipe selection :

 cell.textLabel.text = [recipe1.ingredients objectAtIndex:indexPath.row];

On second Recipe selection :

cell.textLabel.text = [recipe2.ingredients objectAtIndex:indexPath.row];

Modify following function: for (MAC OS)

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

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

    }

OR for iOS:

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

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

After that make sure to reload the table view, once the selection is done. May this will help you.

What you want to do is :

You will be having array full of Recipe Objects & that will be the datasource of your tableview. & when you click on any any of the recipes (name), it will push to another view controller giving the list of ingredients required for that recipe in a tableview (as list).

For this, first create a main view controller with recipeArray (recipe objects) as datasource & list the recipes in table view with recipe name. When you select a recipe ( In didSelectRowAtIndexPath will be called), push to new view controller with the selected recipe object & populate the ingredients list in next tableview controller.

In viewDidLoad Add 1 Line

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

And also Add this below method , So You won't see any additionAL cells which are empty

-(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];
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM