Why is my cell nil - ios

I am writing the code to populate a UITableView from using a CoreData database wrapped in magical record. For some reason though the code docent seem to be working and my cell is considered nil. Why is this happening?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
PassWhizEntity *pazz = [allPasses objectAtIndex:indexPath.row];
cell.textLabel.text = pazz.destinationTeacherAttribute;
//cell.detailTextLabel.text = [self.dateFormatter stringFromDate:pazz.dateAttribute];
if (cell == nil)
{
NSLog(#"cell was nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
it just prints cell was nil and loads a blank UITable. Any ideas?

Your code should be like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSLog(#"cell was nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
PassWhizEntity *pazz = [allPasses objectAtIndex:indexPath.row];
cell.textLabel.text = pazz.destinationTeacherAttribute;
//cell.detailTextLabel.text = [self.dateFormatter stringFromDate:pazz.dateAttribute];
return cell;
}
You are trying to set textLabel of unallocated cell which have no effect on cell. Cell is returned nil because tableview don't have any cell that can be reused, so it is your responsibility to check for nil and make new Cell if needed.

Related

DetailTextLabel is not display in iOS 8 xcode 6

I making the tutorial on running iPhone 5S, Xcode 6 and iOS 8. I want to display the textDetailLabel in a cell table. Can you help me what's the problem here. I already checked the syntax but it doesn't work
- (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];
}
Checklist *checklist = [self.dataModel.lists objectAtIndex:indexPath.row];
cell.textLabel.text = checklist.name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
int count = [checklist countUncheckedItems];
if ([checklist.items count] == 0) {
cell.detailTextLabel.text = #"(No Items)";
} else if (count == 0) {
cell.detailTextLabel.text = #"All Done!";
} else {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d Remaining", count];
}
return cell;
}
Move
static NSString *CellIdentifier = #"Cell";
out of the method, make it public.
When you create the table view write this line where you create the table (in viewDidLoad or your method where you create the table)
[dataTable registerClass:[UITableViewCell class] forCellReuseIdentifier:tableIdentifier];
Remove cell condition from
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
method because your table view cell is not nil and you can't set the cell style.
So you have to write
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

Cell at indexpath 0 not displaying correctly

I have a UITableView with sections inside of them. All works great, but I can't seem to figure out how to customize the very fist index path. For example, I know this is very easily achievable through a normal tableview like so:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
if (indexPath.row == 0)
{
cell.detailTextLabel.text = #"My Custom text";
NSLog(#"%#",pm1.customText1);
}
}
}
I have tried the above implementation and it does not seem to work for me. Is there a different approach for section headers to achieve the same result with a normal tableview? Do I need to inject a "fake" section in my dictionary?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0) {
cell.detailTextLabel.text = #"My Custom text";
NSLog(#"%#",pm1.customText1);
}
return cell;
}
Try this
The way you have it now the first cell is customize only when cell is nil.
You should move it out of the if statement.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//check for first cell
if (indexPath.row == 0) {
cell.detailTextLabel.text = #"My first Cell";
NSLog(#"%#",pm1.customText1);
}else{
cell.detailTextLabel.text = #"Other cells";
NSLog(#"other cells");
}
return cell;
}

iOS 7 TableView dequeue not working

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:

Table View not populating objects

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
I have a tableview with an Array of objects but when I run it. Nothing shows up. Im fairly new to xcode but Im not sure what my mistake is in the code. Can some one help me out?
If the cellForRowAtIndexPath callback is never called, it could be:
you didn't have set the dataSource of your tableview;
you didn't implement the numberOfSectionsInTableView: and/or tableView:numberOfRowsInSection: callbacks.
And if you dequeue your cell, you need to set the text outside the if branch like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
return cell;
}
Change the code like this your code will run,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.GenusNameLabel.text = [[genusName objectAtIndex:indexPath.row] geniusname];
return cell;
}
geniusname is the NSString you stored the name of the person.

UITableViewCell textLabel text wont set

have a UITableViewCell "postCell" that is an IBOutlet and is attached to a cell I have placed in my .xib. To set the text label:
- (void)viewDidAppear:(BOOL)animated
{
postCell.textLabel.text = #"TEXT";
}
Nothing ever appears.
Edit: This cell is not part of a Table View. Every answer I have seen is assuming it is. This is a single UITableViewCell.
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
PostCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"text";
return cell;
}
You can set text label for the cell in method cellForRowAtIndexPath, please see example below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = #"Cell Title";
return cell;
}
There are a few delegate methods that you need to implement. Here are the two most important ones
tableView:cellForRowAtIndexPath:
tableView:numberOfRowsInSection:
Here's a full tutorial on how to implement a table view.
http://www.raywenderlich.com/?p=1797

Resources