Custom UITableViewCell from xib isn't displaying properly - iphone

I've created custom UITableCells a bunch of times and I've never run into this problem, so I'm hoping you can help me find the thing I've missed or messed up. When I run my app, the cells in my table view appear to be standard cells with Default style.
I have SettingsTableCell which is a subclass of UITableViewCell. I have a SettingsTableCell.xib which contains a UITableViewCell and inside that are a couple labels and a textfield. I've set the class type in the xib to be SettingsTableCell and the File's Owner of the xib to my table controller.
My SettingsTableController has an IBOutlet property named tableCell. My cellForRowAtIndexPath contains the following code to load my table view xib and assign it to my table controller's tableCell property:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellSettings";
SettingsTableCell *cell = (SettingsTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"SettingsTableCell" owner:self options:nil];
cell = self.tableCell;
self.tableCell = nil;
NSLog(#"cell=%#", cell);
}
// Configure the cell...
NSArray *sections = [self.settingsDictionary objectForKey:KEY_GROUPS];
NSDictionary *sectionInfo = [sections objectAtIndex:[indexPath section]];
NSArray *itemsInSection = [sectionInfo objectForKey:KEY_FIELDS];
NSDictionary *item = [itemsInSection objectAtIndex:[indexPath row]];
cell.textLabel.text = [item objectForKey:KEY_LABEL_NAME];
cell.labelName.text = [item objectForKey:KEY_LABEL_NAME];
cell.labelUnitsType.text = [item objectForKey:KEY_LABEL_UNITS];
return cell;
}
This is what my xib set up looks like in IB:
When I run my app, the table displays as if all of the cells are standard Default style cells though:
The seriously weird part is though... if I tap on the area of the cell where the textfield SHOULD be, the keyboard does come up! The textfield isn't visible, there's no cursor or anything like that... but it does respond. The visible UILabel is obviously not the UILabel from my xib though because the label in my xib is right justified and the one showing in the app is left justified.
I'm incredibly confused about how this is happening. Any help is appreciated.
EDIT: Here is the code for my SettingsTableCell class:
#interface SettingsTableCell : UITableViewCell {
UILabel *labelName;
UILabel *labelUnitsType;
UITextField *field;
}
#property (nonatomic, retain) IBOutlet UILabel *labelName;
#property (nonatomic, retain) IBOutlet UILabel *labelUnitsType;
#property (nonatomic, retain) IBOutlet UITextField *field;
#end
#import "SettingsTableCell.h"
#implementation SettingsTableCell
#synthesize labelName;
#synthesize labelUnitsType;
#synthesize field;
- (void)dealloc {
[labelName release];
labelName = nil;
[labelUnitsType release];
labelUnitsType = nil;
[field release];
field = nil;
[super dealloc];
}
#end

I don't know why, but I do know that strange things happen while saving the cell in instance variables.
Have you tried loading the cell directly in cellForRowAtIndexPath?
if (cell == nil) {
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyNibName" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = currentObject;
break;
}
}
}
Your complete code for cellForRowAtIndexPath and SettingsTableCell.h/m would be of help.

My first thought (probably wrong!) is that this is a z order issue and that the cells default label is being displayed on top of your text editing control. Hence not being able to see it. I'd guess that it still responds because the touch is being passed through by the label.
Just a guess :-)

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.

nsmutablearray to tableview

I have a nsmutablearray which im trying to display on a table view. i have a uiviewcontroller and added the table view manually. i have added the delegate and source on the xib by draggin it to the file owner. here is my .h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UILabel *label;
IBOutlet UITextField *texto;
IBOutlet UIWebView *link;
//IBOutlet UILabel *links;
IBOutlet UITextView *links;
// IBOutlet UITableView *tableView;
NSMutableArray *jsonArray;
}
#property (nonatomic,retain) IBOutlet UITableView *tableView;
//#property (nonatomic,retain) NSMutableArray *jsonArray;
-(IBAction)button;
-(IBAction)field;
-(void)populateArray;
#end
and here is my .m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
NSString *one;
NSString *jsonreturn;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"www.myphpfile.com"];
jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
NSLog(jsonreturn); // Look at the console and you can see what the restults are
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];
NSLog(#"jsonList: %#", jsonArray);
if(!jsonArray)
{
NSLog(#"Error parsing JSON:%#", error);
}
else
{
// Exception thrown here.
for(NSDictionary *item in jsonArray)
{
NSLog(#"%#", item);
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [jsonArray count];
}
- (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];
}
NSLog(#"Im HERE!!!");
cell.textLabel.text = [jsonArray objectAtIndex:[indexPath row]];
NSLog(#"Cell is %#", [jsonArray objectAtIndex:indexPath.row]);
return cell;
}
my question is, i cant get the cells to show the date, i have the ns logs and it shows that it is pulling it, also im getting a thread 1, breakpoint 1.1 error where the the cell is created and a warning on the same line sayin tableView is local and hiding instance variable, any thoughts? thanks for the help!
update:
I got my program to compile but it throws an exception:
2013-03-26 22:28:48.691 Hello[79888:11303] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
does this have to do with the information im givin the table or im creating the table wrong? thanks again!
If you've added the tableview through the interface builder/storyboard try adding:
#synthesize tableView;
under your #implementation in your .m file. Then make sure you have the tableview connected properly in your IB. (To the View Controller as both data source and delegate and then form the VC to tableview as tableView.)
NSString *one;
NSString *jsonreturn;
The two statements lack standardization.
If you have not using ARC, jsonreturn will leak.
You are getting the warning because you have an instance variable called tableView and also inside the table view datasource methods tableView is passed in as a parameter. Changing this variable name or the instance variable name would solve the problem. Since they both are referring to the same thing, it doesn't matter.
And if its just stopping at a point saying Stopping at Breakpoint 1 you probably have set a breakpoint. Its looks like a blue arrow mark , located on the left hand side of the code. You can disable it by clicking on it again(becomes a lighter shade) or by deleting it- drag the breakpoint out or right click on the breakpoint and delete.
look at the variable name of your tablview
IBOutlet UITableView *tableView
its same as in built method of UITableView uses
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
thats why its giving you warning, "tableView is local and hiding instance variable"
to avoid it just change your UITableView variablename to something "myTableView"
First of all allocate your NSMutableArray, Like this " ArrayName = [[NSMutableArray alloc] init];

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.

Iphone UITableViewCell CustomCell

Attempting to implement a "Simple" a CustomCell,
I have a normal tableViewController that renders fine using the normal "default" methods,
but I need to implement a Custom cell with some UILabel's and a UIImage.
So I created the CustomCell.h, CustomCell.m, CustomCell.xib
The .H
#interface CustomCell : UITableViewCell <UITextViewDelegate>
{
IBOutlet UIImageView *image;
IBOutlet UILabel *name;
IBOutlet UILabel *date;
IBOutlet UILabel *comment;
}
#property (retain,nonatomic) IBOutlet UIImageView *image;
#property (retain,nonatomic) IBOutlet UILabel *name;
#property (retain,nonatomic) IBOutlet UILabel *date;
#property (retain,nonatomic) IBOutlet UILabel *comment;
and .M
#implementation CustomCell
#synthesize image;
#synthesize name;
#synthesize date;
#synthesize comment;
#pragma mark -
#pragma mark View lifecycle
- (id) initWithController: (Controller *) ctnlr
{
ControllerPointer = ctnlr;
return(self);
}
- (void) SetImage:(UIImageView*)Image
{
image = Image;
}
- (void) SetName:(NSString*)Name
{
[Name retain];
[name.text release];
name.text = Name;
}
- (void) SetDate:(NSString*)Date
{
[Date retain];
[date.text release];
date.text = Date;
}
- (void) SetComment:(NSString*)Comment
{
[Comment retain];
[comment.text release];
comment.text = Comment;
}
anyway, when I attempt to create these customcells in cellForRowAtIndexPath (as one would expect might be implemented) I am left with only a blank screen. So obviously I am missing something big... When I created the .XIB file with "Interface Builder" I made sure to connect the "Referencing Outlets" to the appropriate labels and images.
So following the implied logic of the way the Xcode framework appears to work,
I followed the same reasoning (for lack of an exact example) No worky...
Anyway, if there are any IPhone geeks that would like to enlighten me...
(yes, there are no "[something release]" calls, I am not even sure if anything needed to be alloc'd. Please tell me there's just a couple calls I am leaving out, it can't be too much more than something simple like this Right...?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] lastObject];
}
NSUInteger row = [indexPath row];
SnsObject *sObj = [SnsArray objectAtIndex:row];
[cell SetName:[sObj getUserName]];
NSUInteger row = [indexPath row];
SnsObject *sObj = [SnsArray objectAtIndex:row];
cell.name = [[UILabel alloc]init];
cell.name.text = [sObj getUserName];
cell.date = [[UILabel alloc]init];
cell.date.text = [sObj getDateTime];
cell.comment = [[UILabel alloc]init];
cell.comment.text = [sObj getCommentText];
cell.image = [[UIImageView alloc]init];
cell.image.image = [sObj getImageUrl];
return(cell)
}
Thanks in Advance!
There are other issues with the code beyond what mrcrowl mentioned about now needing to "alloc-init" the outlets. In particular, this line:
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] lastObject];
This is not the typical idiom used to load a custom tableview cell from a .xib. First of all, you pass "owner:self", which means you want to hook up the outlet objects in the .xib file with outlet members in your tableviewcontroller object, probably not what you intended.
Second, you're relying on the order of objects returned from loadNibNamed:owner:options:, which while it may work today, may not work tomorrow, or on a new release of iOS.
Instead, the usual idiom is to declare an outlet for the entire tableviewcell in your tableviewcontroller:
(in the .h file):
...
UITableViewCell *tvCell;
...
#property (nonatomic, retain) IBOutlet UITableViewCell *tvCell;
Then in place of your line, you have this:
[[NSBundle mainBundle] loadNibNamed:#"NewsArchiveTitleTvCell" owner:self options:nil];
cell = tvCell;
self.tvCell = nil;
Normally this isn't done with subclassing, notice how I didn't declare the class as CustomCell, but as a vanilla UITableViewCell. So how to you get at those pesky subviews so you can configure them? Using tags is the normal way:
...
#define kMyKewlLabelTag 1
...
UILabel *kewlLabel = (UILabel *) [cell viewWithTag:kMyKewlLabelTag];
kewlLabel.text = [NSString stringWithFormat:#"Hi there from row %d!", indexPath.row];
...
EDIT:
edit: here's a bit more detail, comments are too short to address the "what's going on here?" question. Here's an excerpt from one of my apps that loads the UITableViewCell from a .xib:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyShoppingCartTvCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"ShoppingCartTvCell" owner:self options:nil];
cell = tvCell;
self.tvCell = nil;
}
...
// (insert code to grab model data for this row)
...
UILabel *nameLabel = (UILabel *) [cell viewWithTag:1];
nameLabel.text = itemNameStr;
UILabel *countLabel = (UILabel *) [cell viewWithTag:2];
countLabel.text = [NSString stringWithFormat:#"%d", itemCount];
UIImageView *iv = (UIImageView *) [cell viewWithTag:3];
...
Here's what's going on here:
There is no custom UITableViewCell subclass, there is only a .xib file named "ShoppingCartTvCell.xib" containing a UITableViewCell, and UI elements placed inside the UITableViewCell. UI elements whose data must change per row are assigned a unique tag (the tag field is in the CMD-1 Attributes Inspector in IB) so that your code can get a handle to those objects to change them (customize labels, images, etc). Make sure you don't use "0" since all elements by default have a 0 tag. Also, make sure the Identifier field of the UITableViewCell in CMD-1 Attributes Inspector is the CellIdentifier string.
The File's Owner of the .xib file is your table view controller where you want to display the cell. More precisely, it can be any class containing a IBOutlet UITableViewCell *tvCell; member. It is an instance of this class that you pass in as owner to loadNibNamed:owner:options:. As long as the value of the linked outlet is nil in the owner, when you call loadNibNamed:owner:options, the outlet of the owner is filled in with the object from the .xib (as long as the connection was made in the .xib in IB). Understanding that is a magic moment in Apple programming that opens whole new vistas to you :).
You must set self.tvCell = nil; to prepare for the next cellForRowAtIndexPath that needs to load from the .xib. I also sometimes set to nil before loadNibNamed:owner:options:, I think that's a bit safer actually.
Here's how you go about loading your UITableViewCells from a .xib:
In xcode, add an IBOutlet UITableViewCell *tvCell; to your UITableViewController class (including property declaration if you like)
In your xcode project, create a New File, User Interface, Empty Xib. Open this .xib in IB
In IB, drag a TableViewCell from the Library into your empty .xib file under First Responder
Click File's Owner, CMD-4 (Identify Inspector), and under Class select the class containing the IBOutlet UITableViewCell *tvCell that you added (probably your UITableViewController subclass, the class where you're manipulating your table).
Control-drag from File's owner to the UITableViewCell, and select the outlet you want to hook up. This is the field that will hold the newly-loaded-from-xib UITableViewCell when you call loadNibNamed:owner:options with an instance of File's Owner as the "owner" parameter.
Add UI elements into the UITableViewCell (make sure they're inside the UITableViewCell hierarchy). Any elements that you want to customize per-row require a unique tag value.
follow the recipe I gave above in cellForRowAtIndexPath
Have a magic moment where you start to understand how .xib files and File's Owner objects really work together, and start creating lots of cool UITableViewCells and other custom view objects in IB because it's really easy and way better than creating them in code (IMNSHO).
When you load a UITableViewCell from a .xib, you shouldn't need to create the controls manually.
For example, this kind of thing is unnecessary:
cell.name = [[UILabel alloc]init];
This will replace the label loaded from your xib with a new label that has a zero frame -- that is, the new label will be located at 0,0 and will have no width or height. Hence, no worky.
Assuming you have the xib hooked up correctly to CustomCell's IBOutlets, they controls you are seeking should already be there.
P.S. Forgive me if I am reading too much into your method name, but I don't think this line will work either, because the .image property expects a UIImage:
cell.image.image = [sObj getImageUrl];
Ok... Thanks all for the good input, but sometimes the simplest answer is not only the most eloquent, it's the best... Here's what I found to work,, keeping it as simple as possible, without changing a thing outside of one function.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for(id oneObject in nib)
{
if([oneObject isKindOfClass:[CustomCell class]])
{
cell = (CustomCell*)oneObject;
}
}
}
NSUInteger row = [indexPath row];
printf("MainMessageBoard.m cellForRowAtIndexPath = [%i]\n",row);
SnsObject *sObj = [SnsArray objectAtIndex:row];
cell.Name.text = [sObj getUserName];
cell.Date.text = [sObj getDateTime];
cell.Comment.text = [sObj getCommentText];
cell.Image.image = [self resizeImage: [self imageFromURLString: [sObj getImageUrl]] scaledToSize:CGSizeMake(32.0f, 32.0f)];
cell.CommentCount.text = [NSString stringWithFormat:#"(%d)", [sObj getCommentCount]];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return(cell);
}