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 Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 09-06-2008, 07:24 PM   #1 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default UITableView Tutorial Part #2

The second part of my UITableView series is finished and uploaded.

This tutorial covers how to add a new item button and an edit button to the navigation bar, how to commit editing and row deletion, and how to add new elements to the content array and update the table view after doing so. This tutorial is about 12:00 minutes long and uses the source code from the first part as the starting place.


iPhone Tutorial - UITableViews Part #2 from Nick Myers on Vimeo.
Download the HiDef version to your computer

The next tutorial will probably go over creating custom cells.

You can go ahead and download the source code if you'd like.

Last edited by myersn024; 09-06-2008 at 09:12 PM.
myersn024 is offline   Reply With Quote
Old 09-07-2008, 04:28 AM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Location: Germany
Posts: 154
ChriB is an unknown quantity at this point
Default

Thank you!
ChriB is offline   Reply With Quote
Old 09-11-2008, 01:39 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 160
Vortec4800 is on a distinguished road
Default

Great! I used this tutorial to get my Table view working great. I do have a small problem though, the animation that happens in the simulator when I remove an item from the list is very strange. It almost looks like it mixes the items up but they still appear in the correct order. Does the same thing happen to you, is there a way to clean up the animation?

Looking forward to the next edition! I'd like to make the add button bring up another window (maybe use an animation to flip the box around like your multiple XIB tutorial) which asks for some info before going adding the object. I also want to edit my cell to show some more info, or maybe have it so that when I select an item it brings up a "more info" box with related data about the object.
Vortec4800 is offline   Reply With Quote
Old 09-11-2008, 08:03 AM   #4 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

Same thing happens on the actual device sometimes too. Just play around with the row animations until you find one you like....or one you dislike the least.
myersn024 is offline   Reply With Quote
Old 11-02-2008, 05:28 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 14
spazthecat is on a distinguished road
Default

Great tutorial! Thank you for working on this and your other tutorials. I've found them all very helpful.

I was wondering if you had anything on how to add an insert row at the end of a table view (one with the little green plus sign when you hit the "Edit" button) rather than putting an insert button in the navigation bar? I have a basic table view that when in edit mode, I'd like to have an "Add new item ...." entry at the beginning or end of the data list.

I've figured out how to have a particular row display the green plus sign and load the view I want but I can't figure out how to only display the "Add new item...." row when in edit mode.

Thanks,
Andy
spazthecat is offline   Reply With Quote
Old 12-21-2008, 01:46 AM   #6 (permalink)
New Member
 
Join Date: Dec 2008
Posts: 4
kienNa is on a distinguished road
Default A problem when scrolling with tableview

I am a newbie in iPhone software development. Recently I am working on a small application for iPhone that uses tableview control. My application displayed a list of data fine the first time when it came up, but when I scrolled up and down, the data in some cells got overrided. When I selected those cells, its data was overlapped by data of the next cells. It seemed that when I scrolled, the function - (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath got called many times. Here is the function:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {

static NSString *CellIdentifier = @"MyIdentifier";
//CGRect cellRect = CGRectMake(0.0, 0.0, tableView.bounds.size.width, 100);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...
NSDictionary *itemAtIndex = (NSDictionary *)[dataController objectInListAtIndex:indexPath.row];
NSString *text = [itemAtIndex objectForKey:@"originalQuote"];
CGSize textSize = {tableView.bounds.size.width - 5, 80.0f};
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:13.0f]constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
CGRect contentRect = CGRectMake(5.0, 5.0, tableView.bounds.size.width - 5, size.height);
UILabel *textView = [[UILabel alloc] initWithFrame:contentRect];
textView.text = text;
textView.font = [UIFont systemFontOfSize:13];
textView.lineBreakMode = 0;
textView.numberOfLines = 10;
textView.textColor = [UIColor blackColor];
[cell.contentView addSubview:textView];

[text release];
[textView release];

return cell;
}

Can anyone please show me what I am doing wrong or what may be wrong in my application? Thanks a lot.
kienNa is offline   Reply With Quote
Old 12-22-2008, 09:25 AM   #7 (permalink)
Super cool guy.
 
Join Date: Oct 2008
Location: Summerville, SC
Posts: 29
jeremieweldin is on a distinguished road
Default

You are adding new labels every time the method gets called. Instead, add the labels/controls only when creating a new cell and then call them out by tag in order to modify their values for the current cell being painted.

Code:
#define WHAT_TAG 1

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		
		UILabel *what;
		
		what = [[[UILabel alloc] initWithFrame:CGRectMake(5, 0.5, 240.0, 25.0)] autorelease];
		what.tag = WHAT_TAG;
		what.font = [UIFont boldSystemFontOfSize:14.0];
		what.textAlignment = UITextAlignmentLeft;
		what.textColor = [UIColor blueColor];
		what.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
		what.backgroundColor = [UIColor clearColor];
		[cell.contentView addSubview:what];
		
    }
		
	Gift *gift = (Gift *)[self.gifts objectAtIndex:indexPath.row];
	
	UILabel *whatLabel;
	whatLabel = (UILabel *)[cell viewWithTag:WHAT_TAG];
	whatLabel.text = gift.what;

	
	return cell;
}

Last edited by jeremieweldin; 01-23-2009 at 12:47 AM.
jeremieweldin is offline   Reply With Quote
Old 12-22-2008, 11:00 PM   #8 (permalink)
New Member
 
Join Date: Dec 2008
Posts: 4
kienNa is on a distinguished road
Default

Thank you Jeremieweldin. Yesterday I made it work by disable the reusable cell by setting dequeueReusableCellWithIdentifier:nil and reuseIdentifier:nil. But I think your solution is better, I will give it a try.
kienNa is offline   Reply With Quote
Old 12-30-2008, 01:49 PM   #9 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default Do NOT disable cell reuse!

Quote:
Originally Posted by kienNa View Post
Thank you Jeremieweldin. Yesterday I made it work by disable the reusable cell by setting dequeueReusableCellWithIdentifier:nil and reuseIdentifier:nil. But I think your solution is better, I will give it a try.
kienNa,

You do NOT want to disable cell reuse. This will cause your application to come to a screeching halt at some point. The memory use of your application will rise to the point where it won't run any more, or the system may force it to shut down.

Getting the hang of cell reuse is tricky. I struggled with it also.

What you do is to ask for a re-used cell, and if that fails, create a new cell and new instances of the child views that belong to the cell. You plug the views and other objects into the cell so that it becomes a complete functioning unit.

In your cellForRowAtIndexPath method, you want to set ALL the values for the cell you are creating. The cell may be a new cell, or it may be a cell that has scrolled off-screen and is being recycled. Set tags on any fields that handle touch events, set text field values, etc.

This is quite confusing, but you need to struggle through it and get it working correctly with cell reuse turned on. Once you get the hang of it it's pretty easy.
Duncan C is offline   Reply With Quote
Old 01-23-2009, 12:32 AM   #10 (permalink)
New Member
 
Join Date: Dec 2008
Posts: 4
kienNa is on a distinguished road
Default

Duncan,

Thanks for your advices. I followed your advices as well as Jeremieweldin example and made it work. I created different identifiers for different cells and only created new cells when I could not find the old cells. It seems working.
kienNa is offline   Reply With Quote
Old 01-23-2009, 12:47 AM   #11 (permalink)
Super cool guy.
 
Join Date: Oct 2008
Location: Summerville, SC
Posts: 29
jeremieweldin is on a distinguished road
Default

Great to hear!
jeremieweldin is offline   Reply With Quote
Old 02-19-2009, 03:39 PM   #12 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 24
kevmcd is on a distinguished road
Default

Nice tutorial. Tried to run the sample code and the delete fails.

Looks like the line

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

Is not valid in 2.2.1
kevmcd is offline   Reply With Quote
Old 04-04-2009, 05:21 PM   #13 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 1
Droopy is on a distinguished road
Default

Yeah, the sample code crashes at deleteRowsAtIndexPaths for 2.2 and later:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update, plus or minus the number of rows added or removed from that section (3).'

To fix it, the following line should be moved before the deleteRowsAtIndexPaths line:

[[delegate contentArray] removeObjectAtIndex:indexPath.row];

Last edited by Droopy; 04-04-2009 at 05:30 PM.
Droopy is offline   Reply With Quote
Old 05-17-2009, 10:10 PM   #14 (permalink)
Noobie here
 
Join Date: Mar 2009
Location: Fort Myers
Age: 18
Posts: 53
RobertD63 is on a distinguished road
Default

How would I do this in a top scores kinda thing? I wanna add a top scores part to my Reaction Time App.
RobertD63 is offline   Reply With Quote
Old 05-22-2009, 08:58 PM   #15 (permalink)
Tutorial Author
 
Join Date: Jun 2008
Location: Lahore, Pakistan
Posts: 34
adeem is an unknown quantity at this point
Default

check out series of tutorial @
iPhone SDK Tutorial + Video Tutorial - {Part 1} UITableView using NSArray | iPhone Diary
adeem is offline   Reply With Quote
Old 06-03-2009, 01:35 AM   #16 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 19
robchronos is on a distinguished road
Default

Thx
robchronos is offline   Reply With Quote
Old 02-23-2011, 01:59 PM   #17 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 10
beefer is on a distinguished road
Default

I know this is an ancient thread but I came across it whilst searching for help with this topic. The sample code still works on latest OS and Xcode subject to Droopy's solution.

Last edited by beefer; 02-23-2011 at 02:20 PM.
beefer is offline   Reply With Quote
Reply

Bookmarks

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 466
15 members and 451 guests
alexeir, David-T, Dj_kades, foslock, HemiMG, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, Pauluz85, pipposanta, QuantumDoja, robsmy, sacha1996, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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