Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-21-2011, 05:22 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 32
mr curious is on a distinguished road
Default Editing Table View

Hey all,

I'm implementing an app from a book which displays a list of cities. Using a table view and nav controller. There's an Edit button on the right of the nav controller which when clicked allows the user to either delete a current city or add a new one.

here's the numberOfRowsInSection and cellForRowAtIndexPath, where cities is a NSMutableArray containing the cities.

Code:

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
    // number of rows = number of City objects in citiesArray
    NSInteger count = cities.count; 
    
    if(self.editing) //if edit button was pressed increase count so the 'Add City' row can be added
    {
        count = count + 1; 
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];
    
    if( nil == cell ) 
    { 
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"] autorelease]; 
    }    
    
    if (indexPath.row < cities.count)
    {
        City *thisCity = [cities objectAtIndex:indexPath.row];
        cell.textLabel.text = thisCity.cityName;
    }
    
    
    else
    {
        cell.textLabel.text = @"Add New City...";
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}
In the cellForRowAtIndexPath method, how does the method know to only add the "Add City " option when in edit mode. My understanding of this method that the "add new City" section is only added when theindexPath.row == cities.count. But surely this is true regardless of whether the app os in Edit mode or not??

Thanks
mc
mr curious is offline   Reply With Quote
Old 07-21-2011, 09:04 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 32
mr curious is on a distinguished road
Smile

Ok so I figured it out.
indexPath is an array which starts from 0. So if there are 4 cities, indexPath goes from 0 to 3. but cities.count starts from 1.
so cities.count = 4 and indexPath = 3.
The only time they'll be equal is when the edit button is pressed and cities.count is incremented by 1.

awesome!

mc
mr curious is offline   Reply With Quote
Reply

Bookmarks

Tags
adding table cells, table view

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 406
15 members and 391 guests
7twenty7, blasterbr, buggen, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, n00b, nyoe, pungs, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,901
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:57 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0