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 *开发者_如何学Python)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?
精彩评论