得到警告:UITableView dataSource必须返回一个单元格

时间:2012-05-25 04:06:55

标签: iphone objective-c uitableview

我正在ViewAController:UITableViewController。我还有firstNameCelllastNameCell,如下enter image description here

ViewAController,我做

-(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;  
}

我的问题:为什么我的cells即使他们连接到xib中的cell

也是零

请就此问题向我提出建议。

4 个答案:

答案 0 :(得分:1)

您的cellForRowAtIndexPath方法未正确初始化单元格。

它应包含此代码 -

static NSString *CellIdentifier = @"Cell";

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

答案 1 :(得分:0)

你可能会在笔尖完成加载之前被调用。检查您的-awakeFromNib方法是否已被调用。

答案 2 :(得分:0)

请试试这个:

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

然后使用nib名称初始化单元格。

答案 3 :(得分:0)

以编程方式执行:

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

在使用TableView的ViewAController中,不要忘记导入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;
}