I'm having a really annoying problem that I'm not sure WHY it's happening.
I have the Table view set, I have the correct Identifier in the Cell, but it still gives me "unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard" when I run it.
the code looks like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
return cell;
}
Any help would be great, cause I want to rip my hair. The table is connected to that class, and then Cell has that #"Cell" Identifier
Re-check all 4 steps
STEP 1:
Your cellForRowAtIndexPath should look like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
return cell;
}
STEP 2: Your "ViewController.m" File should look like this
#import "ViewController.h"
#interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
#end
STEP 3:
PLEASE CHECK - Click on view controller move to Connection inspector - See if any other unwanted connections are present in this inspector.
STEP 4: Tableview cell attribute inspector should like this]
Right, you need to register a cell for reuse. This can be done programmatically after you've created your table view like this:
static NSString *cellIdentifier = #"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
Or this can also be done in Interface Builder by selecting the cell and navigating to the attributes inspector and changing the cell's id.
Instead of:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Try:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if that does not work after
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
add:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
You are not checking for cell is allocate or not.. You can try below code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
// add a placeholder cell while waiting on table data
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
}
return cell;
}
In the Document Outline, verify that your prototype UITableViewCell is a child of the UITableView.
In the Attribute Inspector, verify that your Cell Identifier and Cell Style are properly set in storyboard:
In the Identity Inspector, verify that you have the class set to UITableViewCell:
Related
My table simply has row with a UIImageView. When I assign the image directly like the below mentioned way
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = #"cellidentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
// Configure the cell...
cell.imageView.image = [self.backgroundImages objectAtIndex:indexPath.row];
return cell;
}
It returns error:
unable to dequeue a cell with identifier cellidentifier - must
register a nib or a class for the identifier or connect a prototype
cell in a storyboard'.
So I wonder is there a way to ignore to register a nib or a class for UITableViewCell in order to shorten the code in this case?
If you are using custom class for your cell then you should register the class (in viewDidLoad),use registerNib:forCellWithReuseIdentifier: if the cell is made in a xib file.
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
else write cellForRowAtIndexPath tableView delegate as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Configure cell
return cell;
}
You just need to add one line in your viewDidLoad to resolve the error
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cellidentifier"];
}
Put your cell identifier in storyboard as below screenshot.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Songcell"; // copy this identifier and paste it into your storyboard tableviewcell indetifier
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImageView *IMAGEVIEWNAME = (UIImageView*)[cell viewWithTag:YOUR_TAG]; // give tag to your imageview from storyboard and access it here
IMAGEVIEWNAME.image = [UIImage imageNamed:#"YOURIMAGENAME"];
return cell;
}
Give same name in your storyboard tableviewcell identifier
cellidentifier with this name . i just give my cell identifier songcell.
I'm unable to display any accessories in my UITableView cells:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Foo";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Changing the cell styles shows no difference. What's going on here?
Deleting and then creating a new file fixed the problem.
This was a result of having started with an xib file, and then deleted it in favor of doing the whole thing programmatically. Somehow, after deleting the xib, some of the delegation settings stuck.
I have a Custom Cell designed in storyboard, it is inside a UITableViewController which is working fine with the custom cell.
Now, I'm trying to use the same cell on a UITableViewController with a UISearchDisplayController and it is not working.
This is my method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
}
cell.titleLabel.text = object[#"title"];
cell.subtitleLabel.text = object[#"subtitle"];
return cell;
}
It just return white, regular cells, and if I use the default cell.textLabel.text it shows my objects.
Try this, set Tag on storybord
http://i.stack.imgur.com/eVXPE.jpg
image from AppCoda
And change your method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
}
//Change this - START
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
UILabel *subtitleLabel = (UILabel *)[cell viewWithTag:101];
titleLabel.text = object[#"title"];
subtitleLabel.text = object[#"subtitle"];
//Change this - END
return cell;
}
If you have made the custom cell inside a tableview on the storyboard I don't know if you can use it in another tableview either remake the custom cell in the new tableView on the story board or make a seperate custom .xib just for the cell and load them in you cellForRow methods with something like..
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
[tableView registerNib:[UINib nibWithNibName:#"MyCustomCellNib" bundle:[NSBundle mainBundle] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
Or even better, you could just make a small change in your existing code, and hopefully it will work:
From:
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
To:
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Or in Swift
From:
var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
To:
var cell = self.tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
Credits
Trying to set and access nib tableview cell. Basically the same as creating uitableview with a prototype tableviewcell and setting its identifier and accessing it through
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Fill table in with data
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Question is how would you do this as a nib. As nib IB doesn't have prototype option?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
MPMediaItem *song = [self.songsList objectAtIndex:indexPath.row];
NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
NSString *durationLabel = [song valueForProperty: MPMediaItemPropertyGenre];
cell.textLabel.text = songTitle;
cell.detailTextLabel.text = durationLabel;
return cell;
}
-(void)viewDidLoad {
[self.tableView registerNib:[UINib nibWithNibName:#"MusicViewController" bundle:nil] forCellReuseIdentifier:#"Cell"];
}
Create a custom tableviewCell with the nib file.After creation you have three files YourCustomCell.h YourCustomCell.m and YourCustomCell.xib
Add the IBOutlet in YourCustomCell.h and connect them in YourCustomCell.xib with respective labels.
Now in cellForRowAtIndexPath: add the following:
YourCustomCell *yourCustomCell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
yourCustomCell.labelInCustomCell.textLabel.text = songTitle;
yourCustomCell.detailsLabelInCustomCell.textLabel.text = durationLabel;
return yourCustomCell;
Hope this help you.
You make your cell in the xib file and then in code (probably in viewDidLoad) register the nib with registerNib:forCellReuseIdentifier:. In cellForRowAtIndexPath, you just dequeue a cell with the same identifier that you passed to the register method. No need to put in the if (cell == nil) clause, because it will never be nil.
I am using a TableView xib and all the delegates and datasource seem to be running fine.
If I set self.textLabel.text, it displays the generic number of tableviews correctly, but I need to have my custom TableViewCell showing.
I created a HistoryCell.xib that has just a tableviewcell in it.
I created a UITableViewCell class "HistoryCell.h/HistoryCell.m" and it is set as the file owner of HistoryCell.xib.
I connected the UILabels to the HistoryCell.h
UILabel statusLabel
UILabel nameLabel
UILabel timeLabel
In my main ViewController class int he cellForRowAtIndexPath
I am putting in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[HistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.nameLabel.text = #"Donald Duck";
return cell;
}
What am I doing wrong?
Thanks!
You should register the nib like this (probably in viewDidLoad):
[self.tableView registerNib:[UINib nibWithNibName:#"HistoryCell" bundle:nil ] forCellReuseIdentifier:#"historyCellType"];
Then in your cellForRowAtIndexPath method, use this new way where you don't need the if cell == nil clause:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:#"historyCellType" forIndexPath:indexPath];
cell.nameLabel.text = #"Donald Duck";
return cell;
}
You need to deserialize the actual XIB data:
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *cellnib = [[NSBundle mainBundle] loadNibNamed:#"HistoryXIBName" owner:self options:nil];
cell = (HistoryCell *)[cellnib objectAtIndex:0];
}
Otherwise it's just using a "Default" cell.
EDIT: Also, if you are using storyboards, dynamic cell prototypes remove the need to create cells from NIB files (in case that's an option for you.)
EDIT The 2nd:
You can try this as well (this assumes you are using ARC):
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
HistoryCell *aNewCell = [[HistoryCell alloc] initWithNibName:#"HistoryXIBName" bundle:nil];
cell = (HistoryCell *)*aNewCell.view;
}
There's another method using outlets to your cell in the containing view's XIB file that's a bit more involved that I used to use when iOS4 was current. If the edited version isn't' working for you, I'll dig out an old project and remember how I did it.
When you use tableViewCell with xib, while instantiation load from xib.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:#"HistoryCell"
owner:nil
options:nil]lastObject];
}
cell.nameLabel.text = #"Donald Duck";
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellidentifier=#"cell1";
customcell *cell =
(customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell==nil) {
[tableView registerNib:[UINib nibWithNibName:#"Customcell" bundle:nil]
forCellReuseIdentifier:cellidentifier];
cell =
(customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier
forIndexPath:[NSIndexPath indexPathForItem:indexPath.row
inSection:indexPath.section]];
}
return cell;
}