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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 03-17-2010, 11:36 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 3
Question Dynamic Table Cells...NOT JUST HEIGHT

Hello,
I have seen numerous posts regarding how to change the height of a table cell dynamically. However what I am trying to do, is change the cell itself dynamically.

Here is what I am trying to do.
Lets say at Level 1 I have a table view with 2 cells like below:
cell 1:
label 1: "hello"

cell 2:
label 1: "cat"

when the user taps on cell 1 the nav controller loads in a new view which will take them to Level 2
based off of what the user does on the Level 2 view, when they press the "back" nav bar button, I want Level 1 to look like the following:

cell 1:
label 1: "hello"
label 2: "goodbye"

cell 2:
label 1: "cat"

Notice the addition of "label 2" in cell 1

Can anyone suggest a way to do this?

Here are some thoughts I had but do not prefer:
1. have a custom cell with many labels already included and just enable/visible as I need them. Not very savy
2. In the level 2, when the "back" button is pressed, I could auto-generate a new XIB (in essence an XML) file that would describe the new cell. I would probably also need to auto generate a controller .m & .h file.
Then when Level 1 is being displayed, instantiate this object and fill in the appropriate labels. Savy, but very labor intensive.

So are there any thoughts.
Any help would be greatly appreciated!!!
frankmarco_keyw is offline   Reply With Quote
Old 03-17-2010, 02:32 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 40
Default

Quote:
2. In the level 2, when the "back" button is pressed, I could auto-generate a new XIB (in essence an XML) file that would describe the new cell. I would probably also need to auto generate a controller .m & .h file.
That sure is one way to overthink the problem.
bbc z is offline   Reply With Quote
Old 03-17-2010, 03:19 PM   #3 (permalink)
N3RD
 
Join Date: Oct 2009
Posts: 201
Default

Just create two (or more) different composite uitableviewcells & use them when appropriate.
tychop is offline   Reply With Quote
Old 03-17-2010, 05:06 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 791
Default

Upon cell1 click, pass a pointer of itself along to the new view, and create/edit your UI elements as you see fit on it, and call [cell setNeedsLayout] when you pop your view, that should do it.
nobre84 is offline   Reply With Quote
Old 03-17-2010, 08:22 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default This is pretty easy.

Quote:
Originally Posted by frankmarco_keyw View Post
Hello,
I have seen numerous posts regarding how to change the height of a table cell dynamically. However what I am trying to do, is change the cell itself dynamically.

Here is what I am trying to do.
Lets say at Level 1 I have a table view with 2 cells like below:
cell 1:
label 1: "hello"

cell 2:
label 1: "cat"

when the user taps on cell 1 the nav controller loads in a new view which will take them to Level 2
based off of what the user does on the Level 2 view, when they press the "back" nav bar button, I want Level 1 to look like the following:

cell 1:
label 1: "hello"
label 2: "goodbye"

cell 2:
label 1: "cat"

Notice the addition of "label 2" in cell 1

Can anyone suggest a way to do this?

Here are some thoughts I had but do not prefer:
1. have a custom cell with many labels already included and just enable/visible as I need them. Not very savy
2. In the level 2, when the "back" button is pressed, I could auto-generate a new XIB (in essence an XML) file that would describe the new cell. I would probably also need to auto generate a controller .m & .h file.
Then when Level 1 is being displayed, instantiate this object and fill in the appropriate labels. Savy, but very labor intensive.

So are there any thoughts.
Any help would be greatly appreciated!!!

You are getting way, way too complicated. Trying to generate a nibfile on the fly and then parsing that would be insanely difficult if it's possible at all.

The standard init routine for table view cells include 3 different styles that display 2 labels. They are UITableViewCellStyleSubtitle, UITableViewCellStyleValue1, and UITableViewCellStyleValue2. Read the description of those styles and see if they will meet your needs.

Lets' assume that a cell of UITableViewCellStyleSubtitle will meet your needs for a cell with 2 labels.

Now, lets say you have an NSArray of data container objects that contain the attributes of the items you are displaying in your table.

When you first invoke your "level 1" table view, the table view will call the data source delegate's cellForRowAtIndexPath method to ask you to create cells for each entry in your table that will fit on the screen. Your data source object would look at your array of data objects and use the data to create cells and install a single label in each one.

When the user clicks on a cell, your tableView:didSelectRowAtIndexPath: method gets called. You look at the indexPath to figure out which data element they are editing, and push your second view ("level 2") controller to let the user change the settings for that cell. You could pass the data container object for the selected cell to your level 2 controller. Have your level 2 controller change the data object as needed.

When the user clicks the back button on the level 2 view, the system will pop that view controller and display the level 1 view controller again.

The table view in your level 1 view will (again) call the data source method tableView:cellForRowAtIndexPath: for each cell it needs to display.

In your cellForRowAtIndexPath method, look at the data object for that index. See if it has a subtitle or not. If it does not, dequeue/create a cell of style UITableViewCellStyleDefault. If it does have 2 labels, you can dequeue/create a cell of one of the styles with 2 labels, and plug your two label strings into the appropriate elements in the cell.

If the pre-defined cells styles UITableViewCellStyleSubtitle, UITableViewCellStyleValue1, and UITableViewCellStyleValue2 don't meet your needs, you could also create a custom cell in code for that case rather than creating one of the standard types. Or, you could use a trick to load a custom cell you created in advance from a nib file. Do a search in the XCode documentation for the string "Loading Custom Table-View Cells From Nib Files" for an excellent writeup on how you can do this.


Regards,

Duncan C
Duncan C is online now   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 Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 266
17 members and 249 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, Duncan C, F_Bryant, Grinarn, HemiMG, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,761
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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