UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
import
I am trying to implement a custom cell to use in my tableview
This is my custom cell interface class
@interface CustomCell : UITableViewCell {
IBOutlet UILabel *nameLabel;
IBOutlet UITextView *inputText;
IBOutlet UIImageView *image;
IBOutlet UIButton *btnBuy;
}
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UITextView *inputText;
@property (nonatomic, retain) IBOutlet UIImageView *image;
@property (nonatomic, retain) IBOutlet UIButton *btnBuy;
@end
This is my view controller
#import "CustomCellProjectViewController.h"
@implementation CustomCellProjectViewController
@synthesize nameLabel, inputText, image, btnBuy, listData;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
image = [[UIImageView alloc] init];
btnBuy = [[UIButton alloc] init];
inputText = [[UITextView alloc] init];
nameLabel = [[UILabel alloc] init];
NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:image, @"image", inputText, @"text", nameLabel, @"label", btnBuy, @"button", nil];
NSArray *array = [[NSArray alloc] initWithObjects:row1, nil];
self.listData = array;
[row1 release];
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.listData = nil;
}
- (void)dealloc {
[listData release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numb开发者_开发知识库erOfRowsInSection:(NSInteger)section {
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
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;
}
}
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath{
NSUInteger row = [indexPath row];
if (row == 0)
return nil;
return indexPath;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:@"You selected %@", rowValue];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selection" message:message delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alert show];
[message release];
[alert release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kTableViewRowHeight;
}
@end
But I am getting an error:
UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
cell is remaining nil
Can anyone help me please?
As for me, I will write codes for CustomCell manually. Then, I can use the cell in -cellForRowAtIndexPath as following.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initByMyWayWithIdentifier:CustomCellIdentifier] autorelease];
}
// assign necessary information to cell
return cell;
}
I find it very fishy the way you get the cell from the xib file. Don't know if that is the problem (what happens when you debug into that method?).
Does your CustomCell need to be in a separate xib, are you using it in different VCs? If not, you could try to stick it into the xib of your VC and then simply create an IBOutlet and hook it up to your CustomCell.
精彩评论