// actual question
I need help in loading the nib file (tableviewcell) using a different cell Identifier than the one specified in the nib.
// background on what I am doing
I have a custom UITableViewCell with few labels, few images, and few buttons. All this is laid out in a nib file. The content of the tableview cell needs to change dynamically based on certain conditions i.e if landscape a button is not displayed; If there is no content to display in a label then the label is not displayed etc. Adjacent label/view on the left should extend to fill up the space of the missing label/button.
I am able to get this to work. The way I did this is in cellForRowAtIndexPath
I
- remove the views that doesn't need to be displayed using removeFromSuperView
and by
- adjusting the frame
and call setNeedsDisplay
for the view that needs adjusting.
I use the same cell Identifier mentioned in the nib file to dequeue the cell. If the returned cell is nil then I use the loadNibNamed:withOwner:options
to load the nib. However I am afraid that this will cause issues when scrolling back and forth as the cell being reused might not have the views that are required to display content when the conditions aren't met as they might have already been removed.
To fix this issue, I am planning to use a different cell identifier for different conditions that trigger a different look and feel of the cell. Alternatively I am also considering to just hide the view and extend the adjacent view over the hidden view. This way I don't need to deal with different cell identifiers.
Edit2:
My alternative approach of hiding and adjusting frame of adjacent view worked for my needs.
However I still want to find the answer to my actual question I described above.
// potential solution
I was wondering if I could pass the cell identifier to the nib loading code via the options
parameter of loadNibNamed
function. Is this possible? If you guys can provide a sample code to achieve this that would be awesome.
Thanks for your time.
All you need to do is create multiple cells in the nib with different identifiers and then call
dequeueReusableCellWithIdentifier
with the appropriate identifier in order to get a reference to the appropriate type of cell.