Label in CustomCell stays nil - iphone

I created a custom UITableViewCell with an UILabel in it.
In the cellForRowAtIndexPath method I initialise the custom cell and give the UILabel a value.
While the custom cell is loaded (the heights of the cells are higher than default cells), I can't seem to give the UILabel a value.
SBDownloadCell.h
#import <UIKit/UIKit.h>
#interface SBDownloadCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UIProgressView *progressbar;
#property (weak, nonatomic) IBOutlet UILabel *details;
- (IBAction)pause:(id)sender;
#end
SBDownloadCell.m
#import "SBDownloadCell.h"
#implementation SBDownloadCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)pause:(id)sender {
}
#end
SBViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SBDownload";
SBDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[SBDownloadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
SBDownload *dwnld = [[SBDownload alloc] init];
dwnld = [SBdownloads objectAtIndex:indexPath.row];
//cell.title.text = [dwnld name];
cell.title.text = #"Test";
cell.progressbar.progress = [dwnld percentDone];
cell.details.text = [NSString stringWithFormat:#"%f MB of %f MB completed, %#", [dwnld completed], [dwnld length], [dwnld eta]];
return cell;
}
Storyboard
I break just after cell.title.text = #"Test"; and still this is what I see:
What could it be?
note: i use Xcode DP-5 with iOS7

I see your properties are marked with IBOutlet, which means you have an Interface Builder file (either xib or storyboard) with your table view cell. Make sure to give the correct cell identifier in Interface Builder to the prototype cell in your table view. You should not be calling initWithStyle:. If dequeueReusableCellWithIdentifier: returns nil when using a UITableViewController and storyboards, this means incorrect setup, as dequeueReusableCellWithIdentifier: should always return a cell (it creates the new one if it has to).
To elaborate a bit further, when using xibs or storyboards, a table view cell's initWithStyle: will never be called. When a nib is loaded, the correct init method is initWithCoder:.
The problem is in static NSString *simpleTableIdentifier = #"SBDownload";. In your storyboard, you have set up the identifier as SBDownloadCell.

You need to allocate your UILabels and UIProgressView. Right now you set properties for them, but in your -initWithStyle method you need to call things like
self.title = [[UILabel alloc] initWithFrame:etc...];
If you do this for each of your properties on SBDownloadCell, the labels should be allocated properly.

If you custom your prototype cells in Storyboard, you need to present your TableViewController with segue in storyboard, or if you you need to present the TableViewController by programming, you need to use instantiateViewControllerWithIdentifier: method,
- (IBAction)showTableView:(id)sender
{
UIStoryboard *storybaord = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
LJTableViewController *vc = (LJTableViewController*)[storybaord
instantiateViewControllerWithIdentifier:#"TableViewControllerID"];
[self presentViewController:vc animated:YES completion:nil];
}
if you initiate TableViewController with [TableViewContrller new], this will let you get an nil label in tableView:cellForRowAtIndexPath mehtod.

I encounter same problem but my identifier is correct for creating custom cell and show data in IBOutlets custom lable, but after cell allocation lables values are null. So for this I need to add method of tableview for showing lables values
- (void)tableView:(UITableView *)tableView willDisplayCell:(DropViewTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.lblHSNCode.text = objClass.HSNCode;
}

Related

No Visible View in Custom Table View Cell

I am doing an exercise about tableView and tableViewCell. And I am having some problems with custom view cells.
I have created my own custom tableViewCell with .xib file called as newCellView.xib. I have required .h and .m files and I choose super class as UITableViewCell.
In my view controller called as TableViewController I was creating a table with default cells like this.
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = [showNames objectAtIndex:[indexPath row]];
cell.imageView.image = [UIImage imageNamed:[iconArray objectAtIndex:[indexPath row]]];
return cell;
}
Once I have created my own custom view. I imported newViewCell.h into my viewController and I updated the code like this.
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *newCellViewString = #"newCellView";
newCellView *cell = [tableView dequeueReusableCellWithIdentifier:newCellViewString];
//newCellView *cell = [[newCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newCellView];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"newCellView" owner:self options:nil];
cell = (newCellView *)[nib objectAtIndex:0];
}
But when I run the app I see a blank page, like I have no related view. Maybe I forgot some connections to add. I can't even see empty cells to view. Only a white blank page. Any help would be great. Thanks.
EDIT FOR OTHER FILES
Here are my .h and .m files.
#import <UIKit/UIKit.h>
#interface newCellView : UITableViewCell <UITableViewDelegate>
#property (nonatomic, strong) IBOutlet UILabel *nameLabel;
#property (nonatomic, strong) IBOutlet UIImageView *iconImageView;
#property (retain, nonatomic) IBOutlet UIButton *extendButton;
#end
.m file
#import "newCellView.h"
#implementation newCellView
#synthesize nameLabel;
#synthesize extendButton;
#synthesize iconImageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[extendButton release];
[nameLabel release];
[iconImageView release];
[super dealloc];
}
#end
When dealing with custom cell, I'm always trying to encapsulate everything related to the cell in a UITableViewCell subclass (including the NIB / outlets / ..) and use the tableView registerClass: forCellReuseIdentifier: method to tell my table which class to use for its cells.
In your example to do so you could:
In your newCellView.m, add the nib loading in the cell init:
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"newCellView" owner:self options:nil];
self = [nib objectAtIndex:0];
}
return self;
}
Make sure all Outlets connections are correct. (i.e. your UITableViewCell in your Nib is of class NewCellView,..)
Then in the viewDidLoad of your controller you tell your table to use newCellView for its cells:
[yourTableView registerClass:[newCellView class] forCellReuseIdentifier:#"newCellView"];
Finally in the cellForRowAtIndexpath:
newCellView *cell = [tableView dequeueReusableCellWithIdentifier:#"newCellView"];
if (cell == nil)
{
cell = [[NewCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"newCellView]";
}
I think, the problem with your question is that the custom cells are not attached to the custom class cell.
When you create a UITableViewCell class, it does not create a xib along with it.
Then you need to create a xib file, that needs to be attached to the custom class file.
Create an EMPTY NIB file, with no content. Then add a uitablecustomcell through the objects,
and when you do add the new object, GO TO File Inspector and in File Name enter the name newCellView.
Now the custom cells will display EMPTY rows.
Now, add several views to the custom cell and attach those views via IBOutlets created in .h files, namely nameLabel, iconImageView, extendButton.
This is a simple error I have encountered before. You forgot to typecast. it should be:
cell = (newCellView*)[nib objectAtIndex:0];
If this does not resolve your issue make sure you have your xib file set to use the "newCellView" class and not the default "UITableViewCell" class.
Also, if you created a tableview manually and added it as a subview of another view or set it as your view in the loadView method or similar, rather than subclassing UITableViewController make sure you set the frame for the tableview and that you added it as a subview.
You may want to remove the from your newCellView.h class. The delegate for the tableview should not be a view or a subclass of one. Especially not the cell that the tableview will be presenting. The UITableViewController should be receiving the delegate methods.
I found the answer. Everything was working good in my code except the positioning of the cell view's.
Project was using autolayout as a default property. So whenever i see the white page actually cells were out of bounds. I disabled autolayout and set movement properties of the items from size inspector.
Thanks for efforts.

How do I add a UITableView to a parent that contains other objects?

This is essentially the layout I want:
The UITableView at the bottom should accomodate comments to a specific post, adding a row for each comment.
The UITableView at the bottom is wired to commentTable; all other elements are wired accordingly as well.
When I build and run, no errors, but I only see one empty table cell below the post.
I know there's something missing in loading/passing data to my table, but I wonder if someone can give me a direction on how to make this work.
DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController {
IBOutlet UIImageView *postThumbView;
IBOutlet UILabel *postTextLabel;
IBOutlet UIImageView *postAuthorPictureView;
IBOutlet UILabel *postAuthorNameLabel;
IBOutlet UILabel *postTimestampLabel;
IBOutlet UIScrollView *scroller;
IBOutlet UITableView *commentTable;
}
#property (strong, nonatomic) id detailItem;
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void)configureView
{
if (self.detailItem) {
NSDictionary *post = self.detailItem;
NSString *postText = [post objectForKey:#"post_text"];
...
postTextLabel.text = postText;
...
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *post = self.detailItem;
NSDictionary *commentThread = [post objectForKey:#"comment"];
return commentThread.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"commentCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *post = self.detailItem;
NSDictionary *commentThread = [post objectForKey:#"comment"];
NSString *commentText = [commentThread objectForKey:#"comment_text"];
NSString *commentAuthorName = [commentThread objectForKey:#"comment_author_name"];
cell.textLabel.text = commentText;
cell.detailTextLabel.text = [NSString stringWithFormat:#"by %#", commentAuthorName];
return cell;
}
#end
It may be that the table view delegate method's you've written aren't being called. The first thing you should do is set breakpoints inside these methods, run your app, and see if they are being called.
If they're not being called, you may have failed to set your delegate. In this case, it appears that you are not using a discrete UITableViewController, rather you are attempting to have your DetailViewController supply the necessary information for the tableView to work as expected.
First, you need to conform your DetailViewController to the UITableViewDelegate protocol:
#interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Second, you need to actually set the delegate #property of your UITableView. You can do this in interface builder (select the tableview, right click, drag it's delegate property to connect to your DetailViewController, which may or may not be File's Owner). If you'd rather do it in code, you just need to call (early in the VC's life, in viewDidLoad, for example):
self.tableView.delegate = self;
self.tableView.datasource = self;
So... assuming your delegate is all wired up properly, you should then go back and test those breakpoints to see if the table view's methods are being called. If they are being called, the next step would be to evaluate the variables when the breakpoints are called, examine for example if the numbers being return in numberOfRowsInSection and the values in cellForRowAtIndexPath match what you anticipate.
You need to declare your view controller as the delegate and data source for the table view
change this line in your .h file
#interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Then in your viewDidLoad
commentTable.dataSource = self;
commentTableView.delegate = self;
[commentTableView reloadData];
[self configureView];
You can also look in the story board, and connect the outlets n the same way you connected commentTable to your UITableView, but by dragging in the opposite direction and selecting data source and delegate

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath

i have search at https://stackoverflow.com/search?q=UITableView+dataSource+must+return+a+cell+from+tableView%3AcellForRowAtIndexPath ,but i know i need to check if the cell is nil,but my cell is custom,and i am not use xib,i am use storyboard.i am also choice the class CustomPlaceTableViewCell at storyboard.as http://i.stack.imgur.com/g0rRO.png
code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomPlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if ([cell isKindOfClass:[CustomPlaceTableViewCell class]]) { cell = (CustomPlaceTableViewCell *)cell; }
//retrieve the place info
Place *place = [self.placeDataController objectInPlaceListAtIndex:indexPath.row];
//set the cell with place info
if (place.name)
{
cell.nameLabel.text =place.name;
}
else
{
cell.nameLabel.text = #"PortAura";
}
if (place.distance)
{
cell.distanceLabel.text = place.distance;
}
else
{
cell.distanceLabel.text = #"PortAura";
}
return cell;
}
CustomPlaceTableViewCell.m
#interface CustomPlaceTableViewCell : UITableViewCell{
UILabel *nameLabel;
UILabel *distanceLabel;
UILabel *addressLabel;
}
#property (nonatomic) IBOutlet UILabel *nameLabel;
#property (nonatomic) IBOutlet UILabel *distanceLabel;
#property (nonatomic) IBOutlet UILabel *addressLabel;
#end
#implementation CustomPlaceTableViewCell
#synthesize distanceLabel,nameLabel,addressLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
when i run my app ,it give me :
2012-07-02 17:16:26.675 MyAura[2595:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I think You miss return cell in tableView:cellForRowAtIndexPath
try this
return cell;
ans check or use this NSLog(#"description = %#",[cell description]);
if [cell description] is null then change it
CustomPlaceTableViewCell *cell = (CustomPlaceTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Yes your code will get crash even after you are using all suggestions above. Have you checked that your cell is actually being created??
No this is not being created and this method is actually returning a nil value.
Using following line in your code is trying to use reusable cell but what if there is no cell created??
CustomPlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
you have to check this after your code like:
if (cell == nil) {
cell = [[CustomPlaceTableViewCell alloc] init]; // or your custom initialization
}
after this only your cell won't be nil and app won't get crash.
Hope this will solve your issues
I too was facing the same issue but I resolved it.
I am facing this issue because, I created my talbeViewCell in separate Xib file and I made my tableview class as File's owner. But, I haven't linked my cell IBOutlet to my tableview class.
So, linking my tableviewcell Xib to IBOutlet created in my tableview, I resolved this problem.
You will get this issue because your tableviewcell crated in cellforRowAtIndex is nil.
return cell; is missing at end
first i used :
PlaceMasterViewController *placeController = [[PlaceMasterViewController alloc]init];
[self.navigationController pushViewController:placeController animated:YES];
cause me no data in the table,or give me the mistake,so i used:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
PlaceMasterViewController *placeController = (PlaceMasterViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"peoplemastViewControl"];
[self.navigationController pushViewController:placeController animated:YES];
all is OK

UITabBarController with UITableView

i am trying to display a list on my first view so added this in the first.h :
#import <UIKit/UIKit.h>
#interface argospineFirstViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *Journals;
IBOutlet UITableView *myTableView;
}
#property (nonatomic,retain) NSMutableArray *Journals;
#property (nonatomic, retain) UITableView *myTableView;
#end
and then i added this on my first.m :
#implementation argospineFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Journals=[NSMutableArray arrayWithObjects:#"journal1",#"journal2",nil];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
cell.text=[Journals objectAtIndex:indexPath.row];
return cell;
}
I am newbie so i don't really know what kind of connections i have got to make, i am also using a storyboard with a tableview dropped on the first view.
is there something i have to add to the delegate?
Any help?
Thank you for your time
Right click on the tableView in the storyboard. You will see "delegate" and "dataSource" under Outlets. Drag the bubble on the right of those to the view controller icon at the bottom of the view. This will make your viewcontroller the delegate and datasource for the table view if you don't want to do it programmatically.
Do not make property of your table view object.
Also,
in viewDidLoad method write:
myTableView.dataSource = self;
myTableView.delegate = self;
Tell me if it helps!
Use initWithStyle instead of initWithFrame for creating your cell.
In your storyboard, select your table view and open the Connections Inspector. Make sure that the delegate and datasource connections are linked to your argospineFirstViewController object.
in IBOutlet set delegete and datasource of the tableview to filesOwner
you use cell.text for show the data i think its not work in tableview just try this line:-
cell.textlabel.text=[yourArrayname objectatindex:index.row];
no need to connect delegate you already define in protocol.

iOS: Custom UITableViewCell touch receiver confusion

have seen similar questions but couldn't find a definitive answer.
Having mastered regular tables of most types, I am doing some conceptual experiments with custom table cells to get familiar with how this works. I want to have a custom subclass of UITableViewCell which loads a nib to the contentView. I may want to implement different editing styles at some later point but want to reuse the custom view in different parts of my application, however, i'm having problem receiving the didSelectRowAtIndexPath message in my calling UITableViewController.
Here's the hierarchy which is built from a basic view template.
CustomCellViewController: A stock XCode objective-c class sublcassed from UITableViewCell
#interface CustomCellViewController : UITableViewCell {
IBOutlet UILabel *lbl;
}
#property (nonatomic, retain) IBOutlet UILabel *lbl;
#end
#implementation CustomCellViewController
#synthesize lbl;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSArray *a = [[NSBundle mainBundle] loadNibNamed:#"customCellView" owner:self options:nil];
UITableViewCell *tc = [a objectAtIndex:0];
NSLog(#"Cell loaded from nib");
[self.contentView addSubview:tc];
}
return self;
}
.. the other stock methods are unchanged ..
#end
I realise that the init method could be simplified but this is my first attempt.
The XIB file's owner is my custom class (CustomCellViewController), has a UITableViewCell and a label (linked to the outlet 'lbl' on it) positioned half way accross, leaving plenty of the underlying UITableViewCell clickable.
RootViewController is a standard, stock XCode subclass of UITableViewController
RootViewController sets up an instance variable "CustomTableCellController *myCustomCell"
The cellForRowAtIndexPath: is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myCustomCell";
CustomCellViewController *cell = (CustomCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCellViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSLog(#"Creating New cell for position %d", indexPath.row);
} else {
NSLog(#"Reusing cell for position %d", indexPath.row);
}
// Configure the cell.
cell.lbl.text = [NSString stringWithFormat: #"Hi There %d", indexPath.row];
return cell;
}
And in the same RootViewController.m, my didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Cell tapped at row %d", indexPath.row);
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
Designed at present, to purely output a log message when tapped.
numberOfSectionsInTableView returns 1
numberOfRowsInSection returns 50
This all compiles and runs fine, iPhone simulator starts, I see a table on the display, my output log confirms it has created 9 versions of CustomCellViewController and I can see the reuse stack via the NSLog() comments.
I just can't select a row, doesn't matter where I click in the custom cell, nothing gets to my didSelectRowAtIndexPath: in my RootViewController which is where I expect it.
Is it that I have I not set a delegate somewhere and if so, how? Do I need to do this via a first responder? (ie, create a method in my CustomCellViewController class, link the UITableViewCell from the XIB to that method and then call [super didSelectRowAtIndexPath] - but how do I pass the indexPath?
Am I not responding to a message from my XIB in it's owner and then passing it on (is this how I do it?)
I read through all the apple docs to get to this stage but couldn't quite decipher how touch messaging happened.
Slightly confused!
May be you have forget to set data source and delegate if the tableview object set it as like below
tbl.delegate = self;
tbl.dataSource = self;