我希望通过为每个单元格添加标题,日期和图像来自定义AQGridViewCell。
到目前为止我尝试的是:
//查看控制器
- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
static NSString * CellIdentifier = @"CellIdentifier";
IssueCell * cell = (IssueCell *)[self.gridView dequeueReusableCellWithIdentifier: CellIdentifier];
if ( cell == nil )
{
cell = [[IssueCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 72.0, 72.0) reuseIdentifier: CellIdentifier];
}
//This model object contains the title, picture, and date information
IssueModel *m = (IssueModel *)[self.issues objectAtIndex:index];
[cell setIssueModel:m];
return cell;
}
// Cell class
#import "IssueCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation IssueCell
@synthesize issueModel;
- (id) initWithFrame: (CGRect) frame reuseIdentifier:(NSString *) reuseIdentifier
{
self = [super initWithFrame: frame reuseIdentifier: reuseIdentifier];
if ( self == nil )
return ( nil );
self.contentView.backgroundColor = [UIColor redColor];
self.backgroundColor = [UIColor blueColor];
self.contentView.opaque = NO;
self.opaque = NO;
self.selectionStyle = AQGridViewCellSelectionStyleNone;
return self;
}
@end
我的问题是,因为在我访问模型对象之前调用了init,我可以在哪里设置我的单元格的标题,图片和日期?
答案 0 :(得分:1)
您必须在initWithFrame中初始化UI组件。例如:
在IssueCell的界面中添加您想要的UI变量:
@interface IssueCell : AQGridViewCell {
UIImageView *im;
UILabel *dateLabel;
}
- (id) initWithFrame: (CGRect) frame reuseIdentifier:(NSString *) reuseIdentifier
{
self = [super initWithFrame: frame reuseIdentifier: reuseIdentifier];
if ( self == nil )
return ( nil );
self.contentView.backgroundColor = [UIColor redColor];
self.backgroundColor = [UIColor blueColor];
self.contentView.opaque = NO;
self.opaque = NO;
self.selectionStyle = AQGridViewCellSelectionStyleNone;
im = [[UIImageView alloc] initWithFrame:yourImageViewFrameHere];
dateLabel = [[UILabel alloc] initWithFrame:yourLabelFrameHere];
[self addSubview:im];
[self addSubview:dateLabel];
return self;
}
@end
稍后,您将在cellForItemAtIndex方法中指定所需的值。例如:
- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
static NSString * CellIdentifier = @"CellIdentifier";
IssueCell * cell = (IssueCell *)[self.gridView dequeueReusableCellWithIdentifier: CellIdentifier];
if ( cell == nil )
{
cell = [[IssueCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 72.0, 72.0) reuseIdentifier: CellIdentifier];
}
//This model object contains the title, picture, and date information
//
IssueModel *m = (IssueModel *)[self.issues objectAtIndex:index];
[cell.im setImage: m.picture];
[cell.dateLabel setText:[date localizedDescription]];
return cell;
}
不要将模型数据存储在UI组件中。那不是不。保持模型与UI分离。这只是一个伪代码,没有经过测试,因为我这里没有我的mac。
如果有帮助,请告诉我。