2016-08-02 118 views
0

我正在嘗試爲UiCollectionView創建自定義單元格,我需要從後臺更新每個單元格。IOS UICollectionView單元格具有不同的單元格實例

所以我創建了每個單元格Cell_Obj的定製類,並使用定時器從Cell_Obj本身更新單元格內容。

下面的代碼在單元格中添加一個圖像,並在每個2秒鐘內增加一個計數器,並將其顯示在新的單元格標籤上。

在每次使用viewcotroller上的按鈕添加新單元格時,單元格更新,但每個單元格在標籤上獲得相同的值。它假設計數器值不同,因爲每個單元格都是Cell_Obj的不同實例,但每當定時器觸發時,counter值和labelTxt(單元格編號)具有相同的值。

ViewController.h

#import <UIKit/UIKit.h> 
#import "Cell_Obj.h" 

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate> 

@property (weak, nonatomic) IBOutlet UICollectionView *collection; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController(){ 

    NSMutableArray *GridArray; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.collection setDelegate:self]; 
    [self.collection setDataSource:self]; 
    // Do any additional setup after loading the view, typically from a nib. 

     GridArray = [[NSMutableArray alloc] init]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return GridArray.count; 
} 

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: 
- (Cell_Obj *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    Cell_Obj *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    // cell.label.text = @"123"; 

    /*cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"AppIcon.png",indexPath.row]];*/ 
    return cell; 

} 

- (void)addImage 
{ 
    Cell_Obj *cell = [[Cell_Obj alloc] init]; 

    // cell.label.text = @"123"; 

    //cell.label.text = [NSString stringWithFormat:@"%d",[dvrGridArray count]-1]; 
    NSString * txt = [NSString stringWithFormat:@"%d",[GridArray count]]; 
    [cell updateTextLabelName: txt]; 


    [GridArray addObject:cell]; 
    [_collection insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:[GridArray count]-1 inSection:0]]]; 


     /* NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; 
     [arrayWithIndexPaths addObject:cell]; 

     [self.collection insertItemsAtIndexPaths:arrayWithIndexPaths];*/ 

} 


- (IBAction)addClicked:(id)sender { 

    [self addImage]; 

} 
- (IBAction)changeImage:(id)sender { 

    // DVR_Obj *cell = [dvrGridArray objectAtIndex:0]; 
    // [cell changeImage ]; 
    // [_collection reloadData]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; 
    Cell_Obj *cell = [_collection cellForItemAtIndexPath:indexPath]; 
    [cell changeImage ]; 

} 

#pragma mark Collection view layout things 
// Layout: Set cell size 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    //NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row); 

    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGFloat screenWidth = screenRect.size.width; 
    CGFloat screenHeight = screenRect.size.height; 

    CGSize mElementSize = CGSizeMake((screenWidth/4)-2, (screenHeight/4)-2); 
    return mElementSize; 
} 
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 
    return 1.0; 
} 

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { 
    return 1.0; 
} 

// Layout: Set Edges 
- (UIEdgeInsets)collectionView: 
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 
    // return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right 
    return UIEdgeInsetsMake(1,1,1,1); // top, left, bottom, right 
} 

@end 

Cell_Obj.h

#import <UIKit/UIKit.h> 

@interface Cell_Obj : UICollectionViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *imageView; 
@property (weak, nonatomic) IBOutlet UILabel *label; 


- (void)changeImage; 
- (void)updateTextLabelName:(NSString*)str; 
@end 

Cell_Obj.m

#import "Cell_Obj.h" 

static NSString *labelTxt ;// = [[NSString alloc] init]; 
static int counter; 

@implementation Cell_Obj{ 



} 

+ (void)initialize { 
    if (self == [Cell_Obj class]) { 
     labelTxt = [[NSString alloc] init]; 
     counter=0; 

    } 
} 


- (id)initWithFrame:(CGRect)frame 
{ 

    self = [super initWithFrame:frame]; 
    if (self) { 


    } 
    return self; 
} 


- (void)awakeFromNib { 

    _imageView.image = [UIImage imageNamed:@"flower1.png"]; 

    _label.text = labelTxt; 

    [NSTimer scheduledTimerWithTimeInterval:2.0f 
            target:self 
            selector:@selector(updateImage) 
            userInfo:nil 
            repeats:YES]; 
} 


- (void)updateImage 
{ 
    _imageView.image = [UIImage imageNamed:@"AppIcon.png"]; 

    counter++; 
    NSString * txt = [NSString stringWithFormat:@"%@-%d",labelTxt,counter]; 
    _label.text = txt; 
} 

- (void)updateTextLabelName :(NSString*)str 
{ 

    labelTxt = str; 
} 



@end 

回答

1

你的每一個細胞都沒有新的細胞。 Cell_Obj *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];此聲明重新使用標識符爲「單元格」的單元格。要每次創建新單元,請不要通過indexPath,而要通過nil

+0

我已經使用了'nil'但沒有改變。 – CodeDezk

+0

檢查gridArray是否返回正確的計數。 –

+0

如果您需要,我可以爲您提供完整的代碼。 – CodeDezk

相關問題