1

我有ViewAController:UITableViewController。我也有firstNameCelllastNameCell喜欢下面在此处输入图像描述

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即使他们连接到cellxib ,我的也是 nil

请就这个问题给我建议。

4

4 回答 4

1

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

它应该包含以下代码 -

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
于 2012-05-25T04:14:41.750 回答
0

请试试这个:

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

然后用笔尖名称初始化单元格。

于 2012-05-25T07:10:22.043 回答
0

以编程方式进行:

自定义单元格.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;
}
于 2012-05-25T08:37:30.677 回答
0

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

于 2012-05-25T04:10:16.697 回答