UITableViewCell initWithStyle:UITableViewCellStyleSubtitle is not working
When using storyboards and prototype cells, a cell is always returned from the dequeue method (assuming a prototype with that identifier exists). This means you never get into the (cell == nil)
block.
In your case the prototype cell is not defined in the storyboard with the subtitle style, so a subtitled cell is never used, and the detail text label does not exist. Change the prototype in the storyboard to have the subtitle style.
UiTableviewCell with dynamic data init not loading correctly
I have a solution for you.
First, create subclass of
UITableViewCell
(For example, name itDemoTableViewCell
)In
initWithStyle
method ofDemoTableViewCell
, add your images and labels to cell.In
cellForRowAtIndexPath
, you don't need add or remove images and labels. Just change color or set image for them.
UITableViewCell Subtitle not showing up
The cell you have registered is a default styled UITableViewCell, it does not have a subTitle. You can't change the style of the cell once it's created.
You basically have two options:
create a simple UITableViewCell subclass that uses UITableViewCellStyleSubtitle (or any other style of your choice)
@interface MyTableViewCell : UITableViewCell
@end
@implementation MyTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
// overwrite style
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
return self;
}
@end
...
[self.matchCenter registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"Cell"];
Or return to the old style dequeue technique where you create a cell if none could be dequeued
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
/* configure */
}
If you do this, you have to remove the registerClass:forCellReuseIdentifier:
call.
I would go with option one, because most likely pretty soon you will figure out that the built in cells are very limited and you want to add your own views (e.g. labels, image views). And if you use a subclass you can use properties to access those views, and don't have to use hacks like tags to access them.
UITableViewCell subtitle not appearing on load
Your code suggests that you have two separate tables... I would suggest that your problem lies within the differences in how the tables have been configured, possibly in your Storyboard.
Do you, perchance, have one of them set with a prototype with an identifier of "Cell" but a style that isn't UITableViewCellStyleSubtitle? That would make it so that your cell creation method isn't called in one, but only in the other.
UITableViewCell subtitle. Not getting it to work
This happens when your cell is defined using a storyboard prototype. In this case the reusable cells are pre-created using the initWithCoder:
method, so if (cell == nil)
never gets hit. See this question for more information.
Since it appears that you would like to use a cell with a standard style, changing the table to not use a storyboard prototype or setting the prototype to "Subtitle" should fix this problem.
initWithStyle:UITableViewCellStyleSubtitle but subtitle not showing
return cell;
Should be the last line. It returns before setting the detailTextLabel
's text
property.
Also, you should have received a warning from Xcode about "unreachable code."
Subtitle in dequeueReusableCellWithIdentifier
There are two approaches:
The old style approach is to not register any class, NIB or cell prototype, call
dequeueReusableCellWithIdentifier
withoutforIndexPath
:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
Song *song = self.music[indexPath.row];
cell.textLabel.text = song.title;
cell.detailTextLabel.text = song.artist;
return cell;
}As we discussed elsewhere, this assumes that you do not register a class for that reuse identifier.
The alternative is to register your own class in
viewDidLoad
:[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"Cell"];
and then call
dequeueReusableCellWithIdentifier
withforIndexPath
option, but lose the code that manually tests if it isnil
(because it never will benil
):- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Song *song = self.music[indexPath.row];
cell.textLabel.text = song.title;
cell.detailTextLabel.text = song.artist;
NSLog(@"title=%@; artist=%@", song.title, song.artist); // for diagnostic reasons, make sure both are not nil
return cell;
}This obviously assumes that you've implemented a
UITableViewCell
subclass that includes the subtitle (note I'm overriding the style):@interface MyCell : UITableViewCell
@end
@implementation MyCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
@end
Personally, I think designing a cell prototype (which automatically registers the reuse identifier and takes care of all of this other stuff) is much easier. Even the old technique of registering a NIB is easier than the above. But if you want to do it entirely programmatically, those are the two approaches.
Related Topics
Longer Subtitles in Mapview Annotations (Swift)
Save Uiimage Array in Nsuserdefaults
Uitextview as Inputaccessoryview Doesn't Render Text Until After Animation
Non-Main Bundle File as Alert Sound
How to Read References Given by Ptr_Refs in iOS
Drag Uibutton Without It Shifting to Center [Swift 3]
Crashing While Collection View Cell Deletion Because of "Uicollectionviewlayoutattributes"
How to Update a Swiftui View State from Outside (Uiviewcontroller for Example)
Why Obj-C Instance Have 1 Retain Count Just Created
Facebook Sdk Login Throws Error in Swift 2 iOS 9
iOS How to Restart App for Changing Language Swift 4
Issue with Encoding "&" in Urls
Swift 3 - Passing Data Between a View Controller and After That to Another 2
Swift Swipe Navigation Table Views
Writing JSON File Programmatically Swift
Swift Difference Between Var Arr:[String] = [] and Var Arr = [String]()