将TTImageView与AQGridView一起使用

时间:2010-10-31 11:50:09

标签: iphone three20 aqgridview

我创建了一个自定义AQGridViewCell。

通过使用UIImageView一切正常。图像显示并且可以单击,但是当我将UIImageView更改为TTImageView时,我无法单击图像。

与下面相同的例子,只需将imageview更改为UIImageView,将setter消息更改为图像,一切都按预期工作。

这是我的ImageGridCell.h

#import "AQGridViewCell.h"
#import <Three20/Three20.h>

@interface ImageGridCell : AQGridViewCell
{
    TTImageView * _imageView;
 NSString *urlPath;
}
@property (nonatomic, retain) NSString *urlPath;

@end

这是我的ImageGridCell.m

#import "ImageGridCell.h"

@implementation ImageGridCell
@synthesize urlPath;

- (id) initWithFrame: (CGRect) frame reuseIdentifier: (NSString *) aReuseIdentifier
{
    self = [super initWithFrame: frame reuseIdentifier: aReuseIdentifier];
    if ( self == nil )
        return ( nil );

    _imageView = [[TTImageView alloc] initWithFrame: CGRectMake(0, 0, 100, 100)];
 [_imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.contentView addSubview: _imageView];
    return ( self );
}

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

- (CALayer *) glowSelectionLayer
{
    return ( _imageView.layer );
}

- (UIImage *) image
{
    return ( _imageView.image );
}
-(void)setUrlPath:(NSString *)urlpath {
 _imageView.urlPath = urlpath;
}

3 个答案:

答案 0 :(得分:1)

我遇到了同样的情况,这是我带来的解决方案。 我的AQGridViewCell有一个TTImageView和一个UIImageView。 我使用TTImageView加载图像,当它加载时,我将它传递给显示它的UIImageView。

为此,我的AQGridViewCell子类实现了TTImageViewDelegate协议。在我的PostGridViewCell.h中,我有:

@interface PostGridViewCell : AQGridViewCell <TTImageViewDelegate> {
    @private
    TTImageView *ttImageView_;
    UIImageView *imageView_;
}
// @property here …
@end

在我的实现文件中,在initWithFrame:reuseIdentifier:方法中,我初始化了我的ttImageView和我的imageView,但我只将imageView添加为我的单元格的子视图。并且我将ttImageView委托属性设置为self(因此我可以在加载图像时收到通知):

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)aReuseIdentifier {
    if ((self = [super initWithFrame:frame reuseIdentifier:aReuseIdentifier])) {
        // init image
        ttImageView_ = [[TTImageView alloc] initWithFrame:CGRectZero];
        ttImageView_.delegate = self;
        imageView_ = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:imageView_];
        // …
    }
    return self;
}

当TTImaveView加载了图像时,它会调用委托上的imageView:didLoadImage:方法(即PostGridViewCell)。在我的实现文件中,我有这个方法:

- (void)imageView:(TTImageView*)imageView didLoadImage:(UIImage*)image {
    self.imageView.image = image;
    [self setNeedsLayout];
}

有帮助吗? 干杯,
托马斯。

答案 1 :(得分:0)

不太确定“点击”是什么意思 - 就像UIButton一样?当用户点击其中一个图像时,您希望实现什么行为?

我在这里看不到眼前的问题,但我可以向您提供可能有用的信息。

最重要的是,TTImageView和UIImageView都来自UIView,TTImageView不是来自UIImageView(重要的是要注意):

UIImageView : UIView
TTImageView : TTView : UIView

我们不知道UIImageView幕后发生了什么,所以我看不出有什么不同,但文档似乎清楚地表明UIImageView的userInteractionEnabled默认行为是{{ 1}} - 这很奇怪,因为你说它正在使用UIImageView,而不是相反。

您是否尝试在TTImageView上将NO属性设置为userInteractionEnabled

答案 2 :(得分:0)

这里的问题是TTImageView的userInteractionEnabled为YES。因此触摸事件由TTImageView持有,而AQGridView需要触摸单元格视图。只需将userInteractionEnabled = NO添加到单元格中的TTImageView即可。