0

我下载了一个包含下一个类 CHTCollectionViewWaterfallCell.h 的演示,其中包含以下代码:

#import <UIKit/UIKit.h>

@interface CHTCollectionViewWaterfallCell : UICollectionViewCell

@property (nonatomic, copy) NSString *displayString;
@property (nonatomic, strong) IBOutlet UILabel *displayLabel;
@property (strong, nonatomic) NSMutableArray *windows;

@end

和 CHTCollectionViewWaterfallCell.m 具有以下内容:

#import "CHTCollectionViewWaterfallCell.h"

@interface CHTCollectionViewWaterfallCell ()
@end

@implementation CHTCollectionViewWaterfallCell
@synthesize windows;

int i=0;


- (void)setDisplayString:(NSString *)displayString {
    windows=[[NSMutableArray alloc]init];
    if (![_displayString isEqualToString:displayString]) {
        _displayString = [displayString copy];
        //self.displayLabel.text = _displayString;

        int n=[_displayString intValue];
        n=n+1;
        self.displayLabel.tag=n;
        NSString* image = [@(n) description];
        UIImage *img =[UIImage imageNamed:[image stringByAppendingString:@".png"]];
        CGSize imgSize = self.displayLabel.frame.size;

        UIGraphicsBeginImageContext( imgSize );
        [img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        self.displayLabel.backgroundColor=[UIColor colorWithPatternImage:newImage];
        self.displayLabel.userInteractionEnabled=YES;

        UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
        [self.displayLabel addGestureRecognizer:tapGesture];
    }
}

-(void)labelTap{

    NSLog(@"tapped %d",self.displayLabel.tag);
    int n=self.displayLabel.tag;
    NSString *idWindow=[@(n) description];
    [windows addObject:idWindow];
}

这是从 ViewController 调用并显示像 pinterest 这样的视图,但它只是视图,所以我添加了这些行:

UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
            [self.displayLabel addGestureRecognizer:tapGesture];

单击标签时捕获。和方法-(void)labelTap,打印视图的标签被点击,我遇到了问题,因为我想在 nsmutablearray 中添加点击标签的标签,但是如果我在方法中执行分配,-(void)labelTap每次标签单击该数组被删除,我试图将其放入(void)setDisplayString:(NSString *)displayString但由于一个奇怪的原因为标签的每个标签创建一个数组,这意味着将单击的 1 存储在数组中,将 2 存储在其他数组中等等...

好吧,我希望你能帮助我,这是我正在使用的完整演示:

演示

4

1 回答 1

0

问题看起来像您在- (void)setDisplayString:(NSString *)displayString此方法中写在下面的行。因此,每次调用它时,都会初始化可变数组。用一些不同的方法写下下面的行。

 windows=[[NSMutableArray alloc]init];
于 2013-10-17T04:42:50.130 回答