1

私は持っています.私ViewAController:UITableViewControllerも持ってfirstNameCellおり、以下がlastNameCell好きですここに画像の説明を入力

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

私の質問: in xibcellsに接続しているのに、なぜ私は nilなのですか?cell

この問題についてアドバイスをください。

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

プログラムでそれを行います:

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;
}
于 2012-05-25T08:37:30.677 に答える
0

これを試してください:

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

次に、ペン先名でセルを初期化します。

于 2012-05-25T07:10:22.043 に答える
0

nib の読み込みが完了する前に呼び出されている可能性があります。-awakeFromNibメソッドがまだ呼び出されているかどうかを確認します。

于 2012-05-25T04:10:16.697 に答える