I have created a very simple application where I am trying to insert a custom cell in a table view. However, whenever I return an instance of the Custom Cell, one nothing is displayed on the screen and second, the application enters some kind of weird infinite loop. Any help would be much appreciated. I have attached my code in this question.
-- view controller which is returning custom cells ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Try to recover a cell from the table view with the given identifier, this is for performance
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
// If no cell is available, create a new one using the given identifier
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"sample_1ViewController" owner:self options:nil];
for (id currentObject in topLevelObjec开发者_StackOverflow社区ts)
{
if ([currentObject isKindOfClass:[CustomCell class]])
{
cell = currentObject;
break;
}
}
}
// Fill the cell
cell.lbl.text = @"test";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
--- custom cell class ---
@interface CustomCell : UITableViewCell {
IBOutlet UILabel * lbl;
}
@property (nonatomic, retain) IBOutlet UILabel * lbl;
@end
@implementation CustomCell
@synthesize lbl;
@end
----
The custom cell is a UITableCellView resource in the sample_1ViewController.xib file. It contains a UILabel. The identifier for the CustomCell is CustomCell as well.
Please see if you could locate something which might be wrong or tell me of something which I am possibly missing out.
Regards Nitin
Does sample_1ViewController.nib contain an instance of your viewController class?
Your code appears to re-load that entire nib file every time dequeueResuableCellWithIdentifier:
returns nil, and if that nib file contains an instance of the same viewController class, then it will continue to try to reload the nib file indefinitely.
Since all you need to do is return an instance of the cell class, how about this:
First, add a tableCell
instance variable to your viewController class:
@class CustomCell;
@interface MyViewController
{
CustomCell * tableCell;
}
@end
Create the single instance of your cell in your init
method:
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self == nil) { return nil; }
tableCell = [[CustomCell alloc] init];
return self;
}
Be sure to release it in dealloc
:
- (void)dealloc
{
[tableCell release];
}
And now, your delegate method becomes:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableCell.lbl.text = @"test";
return tableCell;
}
It was also my problem. I put the view of the cell in the same xib of the TableView and in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
i called
[[NSBundle mainBundle] loadNibNamed:CELLE_NIB owner:self options:nil];
Thanks guys!
精彩评论