Custom tableviewcell not displaying - iphone

I have created a custom tableviewcell. The class has 3 labels. Using a master view controller template to get started, I changed the default tableviewcell in my storyboard to reference my new custom cell, I also changed the type to custom and the identifer to 'CustomTableCell'. I have also modified my cellForRowAtIndexPath method to the following...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"CustomTableCell";
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Item *currentItem = _objects[indexPath.row];
cell.nameLabel.text = [currentItem name];
cell.vegLabel.text = #"V";
return cell;
}
CUSTOM CELL HEADER FILE
#import <UIKit/UIKit.h>
#interface CustomTableCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *nameLabel;
#property (nonatomic, weak) IBOutlet UILabel *vegLabel;
#property (nonatomic, weak) IBOutlet UILabel *priceLabel;
#end
Eveything seems to be connected properly in my storyboard. When I debug I can see that the cell has the properties of my custom cell. Yet when I run the application each row in blank. The tableviewcell is using the correct identifier in the story board. I just can't see what i'm missing. Any help would be appreciated. Thanks.

You are not loading custom cell from mainbundle. So you need to load it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"CustomTableCell";
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Add this line in your code
cell = [[[NSBundle mainBundle]loadNibNamed:#"CustomTableCell" owner:self options:nil]objectAtIndex:0];
if (!cell)
{
cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Item *currentItem = _objects[indexPath.row];
cell.nameLabel.text = [currentItem name];
cell.vegLabel.text = #"V";
return cell;
}

Related

How to reuse a UITableViewCell subclass in IB

I created a UITableViewCell subclass. In the HomeViewController class that currently uses it, I do this:
#interface: (for HomeViewController)
#property (nonatomic, assign) IBOutlet UITableViewCell *customCell;
#implementation:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomTableViewCellIdentifier = #"CustomTableViewCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomTableViewCellIdentifier];
if (cell == nil) {
UINib *cellNib = [UINib nibWithNibName:#"CustomTableViewCell" bundle:nil];
[cellNib instantiateWithOwner:self options:nil];
cell = self.customCell;
self.customCell = nil;
}
NSUInteger row = [indexPath row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
In the CustomTableViewCell.xib, my File's Owner is HomeViewController and I connect the outlet from File's Owner to the CustomTableViewCell. All of that works fine.
Now I want to have another subclass of UIViewController called DetailViewController to use this cell also. My File's Owner object is already used. I'm not super familiar with creating other objects in order to reuse this cell. Can someone explain what I need to do in this scenario? Thanks.
First, don't create a UINib object every time. Create it once and reuse it. It will run much faster.
Second, it looks like the only property of File's Owner that you're wiring up is customCell. If that's all you need, it would be simpler not to wire up a connection at all. Instead, make sure the cell is the first or only top-level object in the nib (by making it the first top-level object in the Objects section of the nib outline). Then you can access it like this:
+ (UINib *)myCellNib {
static UINib *nib;
static dispatch_once_t once;
dispatch_once(&once, ^{
nib = [UINib nibWithNibName:#"CustomTableViewCell" bundle:nil];
});
return nib;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomTableViewCellIdentifier = #"CustomTableViewCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomTableViewCellIdentifier];
if (cell == nil) {
NSArray *topLevelNibObjects = [self.class.myCellNib instantiateWithOwner:nil options:nil];
cell = [topLevelNibObjects objectAtIndex:0];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

Custom Uicells in UIviewController giving error

I am trying to display a custom TableView on a UIViewController but am getting an error "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:"
I had connected the TableView to datasource and delegate.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Custom";
Custom *cell = (Custom *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:#"Custom" owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[Custom class]])
{
cell = (Custom *) currentObject;
break;
}
}
}
cell.textLabel.text =[arr objectAtIndex:indexPath.row];
please help
#import <UIKit/UIKit.h>
#interface VocaTableCell : UITableViewCell
{
UILabel *vocaLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *vocaLabel;
#end
#import "VocaTableCell.h"
#implementation VocaTableCell
#synthesize vocaLabel;
- (void)dealloc {
[vocaLabel release];
[super dealloc];
}
#end
Your code some wrong. below code refer plz. I've corrected.
Below code refer Befoe, Are you correctly make a customTableCell? below list check plz.
There are several ways to create about custtomTablecell.
My manner is very popular.
CustomTabelCell of File's Owner should be a NSObject.
(Very Important Default is Maybe UITableCell. You Must be a change File's Owner NSObject(NSObject mean is id type!))
CustomTableCell link a Object Class a your CustomTableCell Class.
Referencing Outlets link Not File's Owner, must be Your Directly link a CustomTableCell.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Custom";
Custom *cell = (Custom *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
UINib *nib = [UINib nibWithNibName:#"Custom" bundle:nil];
NSArray *arr = [nib instantiateWithOwner:nil options:nil];
cell = [arr objectAtIndex:0];
}
cell.textLabel.text =[arr objectAtIndex:indexPath.row];
}
It looks like you are not returning the created cell at the end of the method. Add 'return cell;' at the end and the error shall disappear.
Did you add the return statement? And is Custom a subclass of UITableViewCell?

Using CustomCell on tableView, how can I get didSelectRowAtIndexPath called?

I'm populating a UITableView with CustomCells and I'm trying to get didSelectRowAtIndexPath called. Here is the header for the Custom Cell.
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell {
IBOutlet UILabel *bizNameLabel;
IBOutlet UILabel *addressLabel;
IBOutlet UILabel *mileageLabel;
IBOutlet UIImageView *bizImage;
}
#property (nonatomic, retain) UILabel *bizNameLabel;
#property (nonatomic, retain) UILabel *addressLabel;
#property (nonatomic, retain) UILabel *mileageLabel;
#property (nonatomic, retain) UIImageView *bizImage;
#end
Pretty simple and straightforward. I have a detailDisclosureButton I'm adding to the cell as well in the cellForRowAtIndexPath method in the cell as well, and the method accessoryButtonTappedForRowWithIndexPath: is being called, but didSelectRowAtIndexPath is not.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCellView"
owner:self options:nil];
#ifdef __IPHONE_2_1
cell = (CustomCell *)[nib objectAtIndex:0];
#else
cell = (CustomCell *)[nib objectAtIndex:1];
#endif
}
// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
/*
CODE TO POPULATE INFORMATION IN CUSTOM CELLS HERE
*/
#ifdef __IPHONE_3_0
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
#endif
return cell;
}
I put an NSLog inside all the methods as well as break points. The method I'm trying to get called is not, but inside my CustomCell class, the following method is. So is there a way to get didSelectRowAtIndexPath to get called while using a CustomCell?
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
Custom cell or not should not make any difference. Are you in editing mode? If you are, you have to set allowsSelectionDuringEditing = true on the tableView.
if you are use Tableview in your xib..so u want to give tableview's data source and delegate connection in file owner..
Since you say that tableView:accessoryButtonTappedForRowWithIndexPath: is getting called, we know that your tableView's delegate property is properly set. So tableView:didSelectRowAtIndexPath: should be getting called as well. If it isn't getting called, my best guess is that you have a typo somewhere in the method signature. Copy and paste this method signature into your code to make sure you didn't accidentally omit the "tableView:" part or make a capitalization error.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
EDIT: Secondary hypothesis: In the .xib where you've defined your custom cell, make sure "User Interaction Enabled" is checked.
check if you have a UITapGestureRecognizer set for myTableView's parent view ..that is probably over riding the touch event and consuming it.

can't see when using UINib

been confused about this for over two hours, so maybe someone can point me at the right direction...
i have a navigation bar with a tableViewController under it. once the first row is selected i am pushing a new tableViewController that loads up custom table cells with the new and shiny UINib object.
cellForRowAtIndexPath is called and i allocated a new row, set up the values of it's two UILabel correctly, and return the cell.
however - the table view is completely empty. if i replace the custom cell with a regular table cell, i see the cell. what the hell is going on here?
some code:
in viewdidLoad:
self.cellNib = [UINib nibWithNibName:#"DetailCell" bundle:nil];
in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"detailCell";
DetailCell* cell = (DetailCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[self.cellNib instantiateWithOwner:self options:nil];
cell = tmpCell;
self.tmpCell = nil;
}
cell.fieldName.text = #"field title";
cell.fieldValue.text = #"field value";
return cell;
}
and the custom cell (that has a xib file associated with it as well):
#interface DetailCell : UITableViewCell {
IBOutlet UILabel* fieldName;
IBOutlet UILabel* fieldValue;
}
#property (nonatomic, retain) IBOutlet UILabel* fieldName;
#property (nonatomic, retain) IBOutlet UILabel* fieldValue;
#end
thanks for your help.
for anyone following this thread, i found the issue to be with a missing cell identifier. the value you define at rowAtIndex needs to be entered using IB for the xib file. there is an identifier field.

Trouble Reloading UITableView

I am trying to create an iphone application that reads a json feed and I am having some trouble updating a TableView.
This is my TableViewController header file:
#interface LiveNewsController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
NSMutableData *responseData;
NSMutableArray *articleArray;
UITableView *tblView;
}
-(void) refreshPressed: (id)sender;
#property (nonatomic, retain) IBOutlet UITableView *tblView;
#end
These are some parts of my TableViewController implementation file:
#implementation LiveNewsController
#synthesize tblView;
-(id) init {
if(self = [super init])
{
self.title = #"Live News";
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"I am here %d",[articleArray count]);
return [articleArray count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
NSLog(#"cellForRowAtIndexPath called. Data size %d", [articleArray count]);
NSUInteger row = [indexPath row];
cell.textLabel.text = [articleArray objectAtIndex:row];
return cell;
}
-(void) refreshPressed: (id)sender
{
NSLog(#"Reload Pressed. Data size: %d", [articleArray count]);
[tblView reloadData];
}
Here is a screenshot of my .xib file with the connections:
So the main idea is the following:
Fetch articleArray
When refresh is pressed update view (the data is being fetched correctly but the snippet doesn't show it here)
numberOfRowsInSection and CellForRowAtIndexPath are only being called once when the view loads but nothing happens when I press the reload button.
I checked tblView and it is nil. I did some research and read that this is usually caused by some erroneous connections in the .xib file but I have triple checked that and I can't seem to pinpoint the problem.
Any help is appreciated,
Thanks!
You don't need to declare the UITableView tblView, the UITableViewController has already one (and therefore all subclasses too).
Just try [self.tableView reloadData];.