简体   繁体   中英

get warning :UITableView dataSource must return a cell

I am having ViewAController:UITableViewController .I also have firstNameCell and lastNameCell like below 在此处输入图片说明

In ViewAController , I do

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {

    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 50;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{
    if (indexPath.section == 0)
        return firstNameCell;

    return lastNameCell;  
}

My question : why my cells are nil even though they are connecting to the cell in xib

Please advice me on this issue.

Your cellForRowAtIndexPath method does not initialize the cell properly.

It should contain code this -

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

It's possible you are being called before the nib has finished loading. Check to see if your -awakeFromNib method has been called yet.

Please try this:

static NSString *CellIdentifier = @"Cell";
cellVideoOptions *cell =(cellVideoOptions *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

and then init cell with nib name.

Do it programmatically:

CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell
{
   UILabel *firstName;
   UILabel *lastName;
}

@property (nonatomic, retain) UILabel *firstName;
@property (nonatomic, retain) UILabel *lastName;


@end

CustomCell.m

#import "CustomCell.h"

@implementation CustomCell
@synthesize firstName, lastName;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {

    // Initialization code

    firstName = [[UILabel alloc]init];
    firstName.textAlignment = UITextAlignmentLeft;
    firstName.font = [UIFont boldSystemFontOfSize:18];
    firstName.backgroundColor = [UIColor clearColor];

    lastName = [[UILabel alloc]init];
    lastName.textAlignment = UITextAlignmentLeft;
    lastName.font = [UIFont boldSystemFontOfSize:14];
    lastName.backgroundColor = [UIColor clearColor];

    [self.contentView addSubview:firstName];
    [self.contentView addSubview:lastName];
   } 
   return self;
}

- (void)layoutSubviews {
  [super layoutSubviews];

  CGRect contentRect = self.contentView.bounds;
  CGFloat boundsX = contentRect.origin.x;
  CGRect frame;
  frame= CGRectMake(boundsX+7 ,7, 240, 20);
  firstName.frame = frame;

  frame= CGRectMake(boundsX+125 ,25, 220, 20);
  lastName.frame = frame;


}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)dealloc 
{
   [firstName release];
   [lastName release];
   [super dealloc];
}

@end

In your ViewAController with TableView, dont forget import CustomCell.h

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

   static NSString *CellIdentifier = @"Cell";

   CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {

      cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
   }

   // Configure the cell...
   if (indexPath.section == 0) {
    cell.firstName.text = @"firstname";
    // and or cell.firstName.frame = ...
   } else {
    cell.lastName.text = @"lastname"
    // and or cell.lastName.frame = ...
   }

   return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM