iPhone开发 - Custom Cel

时间:2010-07-30 02:03:15

标签: iphone uitableview

我正在尝试创建自己的自定义单元格,但由于某种原因,它没有出现。我读了很多例子,代码看起来(至少对我而言)与其他代码相同。我使用界面构建器重新创建它(我删除了默认视图,添加了一个TableViewCell,将Identifier = CustomCell,并使用valueLabel连接Label)。

可以来帮帮我吗?

提前致谢。

//CustomCell.h
#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell {
    IBOutlet UILabel *valueLabel;
}
@property(nonatomic, retain) IBOutlet UILabel *valueLabel;

@end

//CustomCell.C
#import "CustomCell.h"
@implementation CustomCell

@synthesize valueLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)dealloc {
    [super dealloc];
}

@end


//Where I'm trying to use this cell. Here I imported the .h file (#import "CustomCell.h"):

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

    static NSString *CellIdentifier = @"CustomCell";

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

    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

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

    }

    cell.valueLabel.text = @"Any text";

    return cell;
}

1 个答案:

答案 0 :(得分:0)

你的cellForRowAtIndexPath方法属于表视图控制器吗?那是它的位置吗?