我在这里搜索了很多代码,但没有任何帮助我......
我以编程方式创建了一个自定义TableViewCell,我想将它们与另一个视图连接起来。这是我的代码。
.h文件
// GTCustomCellView.h
#import "GTView.h"
@interface GTCustomCellView : UITableViewCell
@property (nonatomic, retain) GTView* containerView;
@property (nonatomic, retain) GTImageView* commentImageView;
@property (nonatomic, retain) GTTextView* commentView;
@property (nonatomic, retain) GTTextView* dateView;
@property (nonatomic, retain) GTTextView* authorView;
@property (nonatomic, retain) GTView* groupView;
@property (nonatomic, retain) UILabel* mainView;
@end
.m文件
@implementation GTCustomCellView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.layout = [[[GTFlowLayout alloc] initWithSpacing: 0] autorelease];
self.backgroundColor = GTDefaultBackgroundColor;
self.containerView = [[[GTView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, 100.0f)]autorelease];
[self.contentView addSubview:self.containerView];
self.commentImageView = [[[GTImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 70.0f, 70.0f)] autorelease];
[self.containerView addSubview:self.commentImageView];
self.groupView =[[[GTView alloc] initWithFrame:CGRectMake(70.0, 0.0, self.frame.size.width - self.commentImageView.frame.size.width, self.containerView.frame.size.height)]autorelease];
[self.containerView addSubview: self.groupView];
self.authorView = [[[GTTextView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, 20.0f)]autorelease];
[self.groupView addSubview:self.authorView];
self.dateView = [[[GTTextView alloc] initWithFrame:CGRectMake(0.0, 20.0, self.frame.size.width, 20.0f)]autorelease];
[self.groupView addSubview:self.dateView];
self.commentView = [[[GTTextView alloc] initWithFrame:CGRectMake(0.0, 40.0, self.frame.size.width, 60.0f)]autorelease];
[self.groupView addSubview:self.commentView];
}
return self;
}
这是另一个观点的重要部分:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
GTCustomCellView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[GTCustomCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.authorView.text = @"Author Test";
cell.dateView.text = @"Date Test";
cell.commentView.text = @"Comment test";
return cell;
}
我只获得空单元格,我唯一的想法是我使用Views而不是UILabels?
答案 0 :(得分:0)
您是否设置了表视图的委托和数据源:
yourTableView.delegate = self;
yourTableView.dataSource = self;
您应该在视图控制器中执行此操作。
答案 1 :(得分:0)
请使用 - (id)initWithStyle:(UITableViewCellStyle)样式reuseIdentifier:(NSString *)reuseIdentifier而不是initWithFrame
此处的其他信息:Is it ok to use initWithFrame for custom tableviewcell after iPhone SDK 3.0?