Quote:
Originally Posted by frankmarco_keyw
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