My Bronze, silver,Gold solution


#1

Guys,
its amazing to see different solutions for single problem (the best i have learned from fellow ios developer is NSPredicate)

Solution (Bronze)

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"Remove"; }

So here is my Solution (silver)

[code]- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
int source = [tableView numberOfRowsInSection:[indexPath section]];
int current = [indexPath row]+1;

if (source == current) {
    return NO;
}
else {
return YES;
}

}[/code]

[code]- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
int source = [tableView numberOfRowsInSection:[sourceIndexPath section]];
int current = [sourceIndexPath row]+1;

if ([destinationIndexPath row]+1 == source) {
    return ;
}

[[BNRItemStore sharedStore] moveItemAtIndex:[sourceIndexPath row] toIndex:[destinationIndexPath row]];

}[/code]

Solution (Gold)

[code]- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
{
int source = [tableView numberOfRowsInSection:[sourceIndexPath section]];
int lastrow = [proposedDestinationIndexPath row]+1;

if (source == lastrow) {
    NSLog(@"lasttrow");
    return sourceIndexPath;
}
else {
    return proposedDestinationIndexPath;
}

}[/code]


#2

Mine is:

#import "ItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"

@implementation ItemsViewController

- (id)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self){
        //for (int i = 0; i < 5; i++){
            //[[BNRItemStore sharedStore] createItem];
        //}
    }
    return self;
}

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

- (UIView *)headerView
{
    if (!headerView){
        [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
    }
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self headerView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [[self headerView] bounds].size.height;
}

- (IBAction)toggleEditingMode:(id)sender
{
    if ([self isEditing]){
        [sender setTitle:@"Edit" forState:UIControlStateNormal];
        [self setEditing:NO animated:YES];
    }
    else{
        [sender setTitle:@"Done" forState:UIControlStateNormal];
        [self setEditing:YES animated:YES];
    }
}

- (void)addNewItem:(id)sender
{
    //int lastRow = [[self tableView] numberOfRowsInSection:0];
    
    BNRItem *newItem = [[BNRItemStore sharedStore] createItem];
    int lastRow = [[[BNRItemStore sharedStore] allItems] indexOfObject:newItem];
    
    NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0];
    
    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete){
        BNRItemStore *ps = [BNRItemStore sharedStore];
        NSArray *items = [ps allItems];
        BNRItem *p = [items objectAtIndex:[indexPath row]];
        [ps removeItem:p];
        
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Remove";
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == [[[BNRItemStore sharedStore] allItems] count]){
        return UITableViewCellEditingStyleNone;
    }
    else{
        return  UITableViewCellEditingStyleDelete;
    }
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == [[[BNRItemStore sharedStore] allItems] count]){
        return NO;
    }
    else {
        return YES;
    }
}

/*- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == [[[BNRItemStore sharedStore] allItems] count]){
        return YES;
    }
    else{
        return NO;
    }
}*/

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [[BNRItemStore sharedStore] moveItemAtIndex:[sourceIndexPath row] toIndex:[destinationIndexPath row]];
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if ([proposedDestinationIndexPath row] == [[[BNRItemStore sharedStore] allItems] count]){
        return sourceIndexPath;
    }
    else{
        return proposedDestinationIndexPath;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[BNRItemStore sharedStore] allItems] count] + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    
    if ([indexPath row] == [[[BNRItemStore sharedStore] allItems] count]){
        [[cell textLabel] setText:@"No More Items!"];
    }
    else{
        BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
        [[cell textLabel] setText:[p description]];
    }
    return cell;
}

@end

#3

does this solution work if you add more than 1 item at the end that is “no more items”? If you load it up and hit new 5 or 10 times or whatever now you have 10 UIViewTableCells that should always be on the bottom. Therefore you need to be checking for more than just the last item.


#4

@openglprogrammer

I am a little confused at your question. You’re asking for a table that will have one section but will have more than 1 item that is supposed to always be on the bottom? If you know the items you want to place on the bottom and have them always be on the bottom you can check the contents of the cell or one of the variables of the allItems to check whether the target is valid instead of just the last position.

The main logic of the program is to create a cell that indicates that there are no more items below it. Having multiples of these defy the idea of the project and if it does occur in the simulation, it becomes more of an issue where it should be checked and corrected so that multiples of these do not occur.


#5

Here’s mine, not terribly clever or different

[code]// BRONZE CHALLENGE START
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @“Remove”;
}
// BRONZE CHALLENGE END

// SILVER CHALLENGE START
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
int i = [indexPath row] + 1;
if(i== [[[BNRItemStore sharedStore]allItems]count]){
return NO;
}
else {
return YES;
}
}
// SILVER CHALLENGE END

// GOLD CHALLENGE START
// 1 - the last row can’t be indented, making it visually different from other rows
-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
int lastRowNum = [[[BNRItemStore sharedStore]allItems]count];
int indexPathNum = [indexPath row] + 1;
if (lastRowNum == indexPathNum) {
return NO;
}
else return YES;
}
// 2 - the last row cannot be deleted by setting its editing style to NONE
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath{
int lastRowNum = [[[BNRItemStore sharedStore]allItems]count];
int indexPathNum = [indexPath row] + 1;
if (lastRowNum == indexPathNum) {
return UITableViewCellEditingStyleNone;
}
else return UITableViewCellEditingStyleDelete;
}
// 3 - the last row cannot be moved under
-(NSIndexPath
)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
int lastRowNum = [[[BNRItemStore sharedStore]allItems]count];
int proposedRowNum = [proposedDestinationIndexPath row]+1;
if (proposedRowNum == lastRowNum) {
return sourceIndexPath;
}
else {return proposedDestinationIndexPath;}
}
// GOLD CHALLENGE END[/code]