Setting style of UITableViewCell when using iOS 6 UITableView dequeueReusableCellWithIdentifier:forIndexPath: - uitableview

I'm trying to work out how to set the UITableViewCellStyle when using the new methods in iOS 6 for UITableView.
Previously, when creating a UITableViewCell I would change the UITableViewCellStyle enum to create different types of default cells when calling initWithStyle: but from what I can gather, this is no longer the case.
The Apple documentation for UITableView states:
Return Value:
A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.
Discussion:
For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.
Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.
This is how my new cellForRowAtIndexPath looks after implementing the new methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell_identifier";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
The code I have so far works fine but always returns the default style. How can I change this so I can create cells with the other styles such as UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2 and UITableViewCellStyleSubtitle?
I don't want to subclass UITableViewCell, I just want to change the default type as I could do prior to iOS 6. It seems odd that Apple would provide enhanced methods but with minimal documentation to support their implementation.
Has anyone mastered this, or run in to a similar problem? I'm struggling to find any reasonable information at all.

I know you said you didn't want to create a subclass, but it looks inevitable. Based on the assembly code while testing in the iOS 6.0 simulator, UITableView creates new instances of UITableViewCell (or its subclasses) by performing
[[<RegisteredClass> alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:<ReuseIdentifier>]
In other words, the style sent (UITableViewCellStyleDefault) appears to be hard-coded. To get around this, you will need to create a subclass that overrides the default initializer initWithStyle:reuseIdentifier: and passes the style you wish to use:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
// ignore the style argument, use our own to override
self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
if (self) {
// If you need any further customization
}
return self;
}
Also, it might be better to send registerClass:forCellReuseIdentifier: in viewDidLoad, instead of doing it every time a cell is requested:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:<RegisteredClass> forCellReuseIdentifier:<ReuseIdentifier>];
}

dequeueReusableCellWithIdentifier isn't deprecated so you aren't required to use the new dequeueReusableCellWithIdentifier:forIndexPath:.
Use the new way along with the appropriate register method (in viewDidLoad) if you are using a custom cell class but use the old way if you want to use one of the UITableViewCellStyle enums.

You can avoid an extraneous subclass by using the storyboard interface builder:
In the Storyboard view, select the table view cell prototype cell (on the table view)
In the Utilities view, in the Attributes inspector, modify the Style value
(Optionally) Modify other values such as Selection and Accessory
The new iOS 6.0 dequeueReusableCellWithIdentifier:forIndexPath: does use those values when allocating new cells and returning them. (Tested on an iOS 6.0 compilation using Xcode 4.5.2)

Another alternative that saves one file is to create a Nib and use registerNib:forCellReuseIdentifier: instead.
Making the Nib is easy: Create a new .xib file in Interface Builder. Delete the default view. Add a Table View Cell object. Using the Attributes Inspector, change the style for the cell. (Here you also have the opportunity to customize the cell further by adjusting other attributes.)
Then in your table view controller's viewDidLoad method call something like:
[self.tableView registerNib:[UINib nibWithNibName:#"StyleSubtitleTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"Cell"];

Bolot's answer is the correct. Simple and you don't need to create any XIB file.
I just wanted to update his answer for whoever is doing it using Swift instead of Objective-C:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .value1, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

My solution to this is to call initWithStyle: reuseIdentifier: after I've obtained it using [self.tableView dequeueReusableCellWithIdentifier:#"cellId" forIndexPath:indexPath]. After all, init is just another selector, and the compiler makes no restrictions on calling it on an already initialised object. It will however complain about not using the result of calling init, so I do:
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:#"cellId" forIndexPath:indexPath];
cell = [cell initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cellId"];
I imagine this won't work in Swift...

Related

How to initialize cells with UITableViewController's new behavior in terms of cell reusing

It's been a while that I haven't coded iOS, and I'm witnessing some new behavior and would like to know how it works.
It used to be that cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath] would return a cell or not, but the code examples that I'm seeing now lack the initialization part:
if (cell == nil) {
cell = [MyCellClass new];//and I think somehow registering the cell with the identifier
//Some code here, for example:
//[cell.button addTarget:self action:#selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
}
Is it going to do these initializations every time now, without checking the existence of cell?
Edit: more details about my use case:
My cell has a Nib file and I'm using the new [[self tableView] registerNib:[UINib nibWithNibName:#"cell" bundle:nil] forCellReuseIdentifier:#"cell"] in my ViewController's viewDidLoad. My ViewController is not present in any Nib/StoryBoard.
dequeueReusableCellWithIdentifier:forIndexPath: is guaranteed to return a cell, and always has been (it was added in iOS 6).
You're remembering dequeueReusableCellWithIdentifier: which was used prior to iOS 6, which did not have the same guarantee. The new API is much nicer to use :)
When using registerNib:forCellWithReuseIdentifier: cell creation is taken care of automatically.
From the docs:
Prior to calling the dequeueReusableCellWithReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerClass:forCellWithReuseIdentifier: method to tell the collection view how to create a new cell of the given type. If a cell of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a new cell object automatically.

Can I safely use dequed tableView cell as prototype

I am using prototype cell to calculate real cell height. Since I'm using storyboard I have chosen to create prototype cell by dequeing it. Example code:
- (MyCell *)prototypePriceOptionCell
{
if (_prototypeCell == nil) {
_prototypeCell = (MyCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
return _prototypePriceOptionCell;
}
This prototype is never returned to tableView in cellForRowAtIndexPath method.
Will it be dequed by table view to reuse and show it in table view?
You technically could, from the docs:
If you registered a class for the specified identifier and a new cell
must be created, this method initializes the cell by calling its
initWithStyle:reuseIdentifier: method. For nib-based cells, this
method loads the cell object from the provided nib file. If an
existing cell was available for reuse, this method calls the cell’s
prepareForReuse method instead.
But it also states that it can return nil under some circumstances.
However, I wouldn't use your approach, not because it wouldn't work but because it's not supposed to be used like that. This means that you have weak code relying on a feature it's not supposed to be used like that.
My suggestion, instantiate your cell manually calling the appropriate methods. Also, I like to have my cells on separate xibs, away from storyboards.
Edit:
Answering your question at the end, yes, the cell can and will be dequeued by the table view if you call dequeue in your cellForRowAtIndexPath

Lazy loading custom subviews for UICollectionViewCell

In -collectionView:cellForItemAtIndexPath: I’m adding custom subviews to the UICollectionViewCell like this:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *const cellIdentifier = #"cellIdentifier";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
MyCustomViewClass *carouselView = [[MyCustomViewClass alloc] init];
[cell.contentView addSubview:carouselView];
return cell;
}
According to the documentation, dequeueReusableCellWithReuseIdentifier:forIndexPath: “dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.”
The problem is that my implementation of cellForItemAtIndexPath is constantly creating new instances of MyCustomViewClass. Even though instances of the latter are removed from the collection view when they’re out of the screen, it still seems wrong to create a new one every single time dequeueReusableCellWithReuseIdentifier:forIndexPath: is called.
My question is, given that MyCustomViewClass instances are graphics-intensive and take up memory, what’s the best way to lazily load them? Do I have to implement my own queue? Or shall I make it a subclass of UICollectionViewCell?
Because you did this MyCustomViewClass *carouselView = [[MyCustomViewClass alloc] init];, everytime the system call dequeueReusableCellWithReuseIdentifier:forIndexPath: it will create a new instance of MyCustomViewClass for you. So what you need to do is to check whether an instance of MyCustomViewClass has already been added to that cell, if not then create a new one.
I have no idea about what your MyCustomViewClass does but you can create a custom UICollectionViewCell that has already that CustomViewClass associated.
If your CustomViewClass extends UIView is simpler. And if you are using storyboards it is even more simple. In your storyboard you don't need to create a custom UICollectionViewCell just for that. You can drag an UIVIew to your CollectionViewCell and set the customView to MyCustomViewClass. This way it will only be created once and then it will be reused.
If your MyCustomViewClass has some kind of state (imagine that is a status bar with a percentage) you can reset that state you have to extend the UICollectionViewCell and override prepareForReuse.

Initializing a custom UICollectionViewCell

I have a custom UICollectionViewCell that has a custom background view which is drawn using one of several colour schemes. The colour scheme for the background view is set in my custom initializer -(id)initWithFrame:andColourPalette: for the View.
I have a similar custom initialiser in my UICustomViewCell subclass but I can't figure out how to call this initialiser when I am setting up the cell in cellForItemAtIndexPath:
Can anyone help me do this? Or offer alternative solution for passing this Dictionary of colours into the Cell to pass on to the subView?
EDIT to show more detail:
This is what I have in my UICollectionView VC:
In ViewWillAppear:
[self.collectionView registerClass:[OPOLawCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
self.colourPalette = [OPOColourPalette greenyColourPalette];
In cellForItemAtIndexPath:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
OPOLawCollectionViewCell *lawCell = (OPOLawCollectionViewCell *)cell;
MainLevel *level = self.collectionData[indexPath.row];
lawCell.delegate = self;
lawCell.colourPalette = self.colourPalette;
In my Custom UICollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// get background view
OPOLawBook *lawBookView = [[OPOLawBook alloc]initWithFrame:CGRectMake(0, 0, 200, 265) andColourPalette:self.colourPalette];
But that doesn't work - I guess because the propertys are not set up.
If I change the last line to this, then it works fine:
OPOLawBook *lawBookView = [[OPOLawBook alloc]initWithFrame:CGRectMake(0, 0, 200, 265) andColourPalette:[OPOColorPalette greenyColorPalette]];
So i guess I need to use a custom intialiser here but I cant figure out how to call it , or from where...
Thanks
Yuo have to register your customCells in collectionView:
[self.collectionView_ registerClass:[YourCustomClass class]
forCellWithReuseIdentifier:#"CustomCell"];
And then in your method cellForItemAtIndexPath:
YourCustomClass *cell = (YourCustomClass *)[collectionView
dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
It is done because collectionView might have 1000 cells and 10 visible. You don't keep all of them initialized and reuse when possible.
EDIT
You should set colorPaletter after you deque the reusable cell. Think of it as a container which can hold any color. You need to determine (by indexpath) what color to paint.
You shouldn't do below if your custom cell is in the Storyboard,
[self.collectionView registerClass:[OPOLawCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
Because Storyboard take responsibility to register Cell_ID own.
Now, It will conflict to be generated invalid Cell if you use both.
Way off, every answer. The questioner is looking for a way to uniquely identify each cell upon initialization, which happens prior to dequeuing a cell, and prior to a cell's access to its index path property.
The only way to do this is to assign a unique reuse identifier to every cell based on what the index path value will be (assuming you will know what that will be—and, in your case, you will); then, when dequeuing the cell, use the index path to find the cell with the corresponding reuse identifier.
Does this negates the purpose of reuse identifiers? Absolutely not. You'll be reusing that cell every time you need to use it again. Reuse identifiers were not meant to limit you to a cookie-cutter cell for every cell in your collection view; they are also intended to be "unique use" identifiers.

Added table view cells follow 1 custom style?

I am wanting to create a custom UITableView cell. I would like to know how to do this. I understand how to actually create it and write code for it, but how can i create 1 style and then when i have more cells added, i want the same style. How can i do this? Is there a way to create 1 custom cell and have all the other cells that i want to add later follow this cells style?Thanks for the help!
In my projects I'm implementing method that creates custom style programmatically. Also it is possible to make custom cell via IB and when you need just take custom cell from it.
Don't forget that if you will write your code correctly then your cells will be reused and that method will be called only for number of cells that are visible in your table view.
may be this can help you http://iphone-bitcode.blogspot.com/2011/06/custom-tableview-cell.html
Write a separate .h/.m/.xib for the cell, and in the .xib set File's Owner to the class you want multiple copies of it in (your table view controller class, most likely). Attach it to an IBOutlet you created in the table view controller for new cells.
Then, each time you want a cell, try and dequeueReusableCellWithIdentifier: on your tableView, and if that doesn't work (you have no reusable ones), make a new cell using your custom class by simply loading the nib file. It will automatically create an instance of the cell and attach it to your IBOutlet, and then just retain the cell and set the outlet back to nil for the next time you need to create a cell. Essentially, I mean this (I have an IBOutlet UITableViewCell *cellOutlet):
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = #"CustomCell";
UITableView *cell = [self.tableView
dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"MyCustomTableViewCell"
owner:self options:nil];
cell = cellOutlet;
self.cellOutlet = nil; // autoreleases
cell.reuseIdentifier = reuseIdentifier;
}
// configure the cell here
return cell;
}

Resources