Eliminating Extra Separators


#1

After agonizing over the three challenges for a few hours today (which involved lots of Googling, looking at your answers, taking snack breaks, etc.), I finally came up with something that looks like this:

See all those extra separators at the bottom? I did some more Googling and found that you can get rid of them by adding a footerView in viewDidLoad:

// This will remove extra separators from tableview
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

However, I’m wondering: Is that the generally accepted way to do that? Or is there something else I should do to get rid of them? Also, I’m happy to answer any questions about my solution. The only file I worked with was BNRItemsViewController.m and I used 3 sections for my tableView (the third section holds the last cell that says “No more items!”).


#2

Hi. I did not run across any extra table cells and without seeing your code I cannot say why yours was showing that. There is a solution I posted here about the “No more items” row but if you look at the next chapter’s silver solution I provided you will see a better way (others may have as well). The only reason I can think of for you having extra cells is if the number of rows in a section you provide is more than the data you have. Again without your code it is hard to suggest what is happening for you.


#3

Thanks C6silver, I’ll check out the Silver solution for the next chapter! As for my cell separator issue, there are no extra rows/cells…just extra separators. I found this line in the UITableView Class Reference under “separatorInset” -

So, judging by that last sentence, I guess those extra separators are normal. Still seems weird though.


#4

[quote=“BeckyH”]Thanks C6silver, I’ll check out the Silver solution for the next chapter! As for my cell separator issue, there are no extra rows/cells…just extra separators. I found this line in the UITableView Class Reference under “separatorInset” -

So, judging by that last sentence, I guess those extra separators are normal. Still seems weird though.[/quote]

I think the quandary here is around the transparency I am seeing from your screen shot. There are indeed rows that extend to the bottom of the phone that are empty but your pic is showing them as transparent which is the part I am finding strange. As far as empty rows you can set the height of the table so that you could have something else right below it it wanted and thus wouldn’t have empty cells running the height of the phone but again the transparency I see from your pic is odd.

Just after I posted this I wondered whether that was transparency or are you inserting a row image? I thought that might be the background at first but now I am not sure. If that is an image you inserted in the row then it does make sense and again you can adjust the table height if you want.


#5

Ok, I’m home from work and can finally post my code! The rows shouldn’t be transparent…the image is set as as the tableView’s backgroundView. The worst part about my solution is that I do the separation of the “under $50” and “over $50” in two places: in tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath. That just seems horribly inefficient. Not sure how else to do it without meddling with the model though.

BNRItemsViewController.m

#import "BNRItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"


@implementation BNRItemsViewController

- (instancetype)init
{
    // Call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        for (int i = 0; i < 6; i++) {
            [[BNRItemStore sharedStore] createItem];
        }
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [self init];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = [[BNRItemStore sharedStore] allItems];
    NSMutableArray *underFifty = [[NSMutableArray alloc] init];
    NSMutableArray *overFifty = [[NSMutableArray alloc] init];
    
    for (BNRItem *item in array) {
        if ([item valueInDollars] <= 50) {
            [underFifty addObject:item];
        }
        else {
            [overFifty addObject:item];
        }
    }
    
    
    if (section == 0) {
        return [underFifty count];
    }
    
    if (section == 1) {
        return [overFifty count];
    }
    
    if (section == 2) {
        return 1;
    }
    
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        return 44;
    } else {
        return 60;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [[BNRItemStore sharedStore] allItems];
    NSMutableArray *underFifty = [[NSMutableArray alloc] init];
    NSMutableArray *overFifty = [[NSMutableArray alloc] init];
    
    for (BNRItem *item in array) {
        if ([item valueInDollars] <= 50) {
            [underFifty addObject:item];
        }
        else {
            [overFifty addObject:item];
        }
    }

    // Get a new or recycled cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    // Set the text on the cell with the description of the item
    // that is at the nth index of items, where n = row this cell
    // will appear in on the tableview
    if (indexPath.section == 0) {
        BNRItem *item = underFifty[indexPath.row];
        cell.textLabel.text = [item description];
        cell.textLabel.font=[UIFont systemFontOfSize:20];
        return cell;
    }
    
    if (indexPath.section == 1) {
        BNRItem *otherItem = overFifty[indexPath.row];
        cell.textLabel.text = [otherItem description];
        cell.textLabel.font=[UIFont systemFontOfSize:20];
        return cell;
    }
    
    if (indexPath.section == 2) {
        cell.textLabel.text = @"No more items!";
    }
    return cell;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return @"Items Under $50";
    }
    if (section == 1) {
        return @"Items Over $50";
    }
    
    return @"";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    
    self.tableView.backgroundView = imageView;
    self.tableView.contentInset = UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);
    
    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

@end

#6

It being an image solves that mystery on my side. The behavior then is as I would expect it.

As for the under and over $50. You can see the way I approached it via the link below. I didn’t change the model but rather created two arrays which were handled via the getter.

viewtopic.php?f=489&t=8099#p23039