Resolved [Table Views] Adding an extra "new item" row at the end.

Discussion in 'iOS Programming' started by CodeBreaker, Sep 29, 2011.

  1. CodeBreaker, Sep 29, 2011
    Last edited: Sep 29, 2011

    CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #1
    Hi,
    I am just starting off with Table Views. So I have created a simple Navigation based App, with a table view that can move and add rows. I got the delete and move working but I'm stuck at inserting.

    When in the editing mode, I want the last row of the table to be named as "Add new" with the "+" badge next to it. In other words, UITableViewCellEditingStyleInsert should be returned for this cell/row. But I'm not able to do it. The cell "New Task" displays fine, but the Insert Style is never returned because the index path.row value never becomes equal to that row.

    Here's my code/root controller:

    Code:
    #import "RootViewController.h"
    
    @implementation RootViewController
    
    @synthesize list;
    
    - (IBAction)editTouched:(id)sender {
        [self.tableView setEditing:!self.tableView.editing animated:YES];
        if (self.tableView.editing) {
            [self.tableView reloadData];
            self.navigationItem.rightBarButtonItem.title = @"Done";
        } else {
            [self.tableView reloadData];
            self.navigationItem.rightBarButtonItem.title = @"Edit";
        }
    }
    
    
    - (void)viewDidLoad
    {
        NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"Hello", @"World", nil];
        self.list = array;
        [array release];
        
        UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editTouched:)];
        self.navigationItem.rightBarButtonItem = editButton;
        [editButton release];
        
        [super viewDidLoad];
    }
    
    
    
    /*
     // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations.
    	return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
     */
    
    // Customize the number of sections in the table view.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (self.tableView.editing) {
            NSLog(@"(in editing) The count is now %d", [self.list count] + 1);
            return [self.list count] + 1;
        } else {
            NSLog(@"(not in editing) The count is now %d", [self.list count]);
            return [self.list count];
        }
    }
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.showsReorderControl = YES;
        }
        NSUInteger row = [indexPath row];
        if (self.tableView.editing && row == [self.list count]) {
            cell.textLabel.text = @"Add new";
            return cell;
        }
        cell.textLabel.text = [self.list objectAtIndex:row];
        return cell;
    }
    
    
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the specified item to be editable.
        if (indexPath.row == [self.list count]) {
            return NO;
        }
        return YES;
    }
    
    
    /*
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete)
        {
            // Delete the row from the data source.
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        else if (editingStyle == UITableViewCellEditingStyleInsert)
        {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }   
    }
    */
    
    
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
        NSUInteger fromRow = [fromIndexPath row];
        NSUInteger toRow = [toIndexPath row];
        if (fromRow != ([self.list count]) && toRow != ([self.list count]) && toRow < ([self.list count])) {
            id object = [[self.list objectAtIndex:fromRow] retain];
            [self.list removeObjectAtIndex:fromRow];
            [self.list insertObject:object atIndex:toRow];
            [self.tableView reloadData];
        }
    }
    
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"(indexpath for style) %d", indexPath.row);
        if(!self.tableView.editing || !indexPath) {
            NSLog(@"(indexpath for style) %d", indexPath.row);
            NSLog(@"None");
            return UITableViewCellEditingStyleNone;
        } else if (self.tableView.editing && indexPath.row == ([self.list count])) { //never goes here
            NSLog(@"(indexpath for style) %d", indexPath.row);
            NSLog(@"Insert Style");
            return UITableViewCellEditingStyleInsert;
        } else {
            NSLog(@"(indexpath for style) %d", indexPath.row);
            NSLog(@"Delete");
            return UITableViewCellEditingStyleDelete;
        }
        return UITableViewCellEditingStyleNone;
    }
    
    
    
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the item to be re-orderable.
        if (indexPath.row == [self.list count]) {
            return NO;
        }
        return YES;
    }
     
  2. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #2
    You're returning NO for the "Add New" row in this method:

    Code:
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the specified item to be editable.
        if (indexPath.row == [self.list count]) {
            return NO;
        }
        return YES;
    }
    
    This means your add new row isn't editable, and your table view won't request an editing style for it.
     
  3. CodeBreaker thread starter macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #3
    D'oh :D Thanks a lot !! I wasted 2 hours on this :)
     

Share This Page