2016-12-02 8 views
2

https://github.com/cwRichardKim/TinderSimpleSwipeCardsを使用しています。私はJSONからデータが来るviewControllerでArrayを作成しました。その数のカードだけがJSON配列のデータとして作成されることを願っています。私は最後の3日からこれをやっていますが、まだ成功していません。ここtinderSwipeCards配列集団

はcwRichardKimが提供するビュークラスは

- (id)initWithFrame:(CGRect)frame 
{ 

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

    exampleCardLabels = [NSArray new]; //%%% placeholder for card-specific information 
    loadedCards = [[NSMutableArray alloc] init]; 
    allCards = [[NSMutableArray alloc] init]; 
    cardsLoadedIndex = 0; 
    [self loadCards]; 
} 
return self; 
    } 

    //%%% sets up the extra buttons on the screen 



// to get rid of it (eg: if you are building cards from data from the internet) 
     -(DraggableView *)createDraggableViewWithDataAtIndex:(NSInteger)index 
     { 

    //MGInvitationModel *model = self.exampleCardLabels[index]; 

     DraggableView *draggableView = [[DraggableView alloc]initWithFrame:CGRectMake(0, -50, CARD_WIDTH, CARD_HEIGHT)];//[[DraggableView alloc]initWithFrame:CGRectMake((self.bounds.size.width)/2, (self.bounds.size.height)/2, CARD_WIDTH, CARD_HEIGHT)]; //[[DraggableView alloc]initWithFrame:CGRectMake(0, -40, CARD_WIDTH, CARD_HEIGHT)]; 
    //draggableView.information.text = [exampleCardLabels objectAtIndex:index];  //%%% placeholder for card-specific information 



    draggableView.delegate = self; 
    draggableView.layer.cornerRadius = 10.0f; 


    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 280, 150, 50)]; 
    nameLabel.font = [UIFont fontWithName:@"Arial" size:12.0f]; 
    nameLabel.text = @"NIVESH"; 
    [nameLabel setTextAlignment:NSTextAlignmentLeft]; 
     nameLabel.textColor = [UIColor blackColor]; 

[draggableView addSubview:nameLabel]; 
return draggableView; 



} 




    //%%% loads all the cards and puts the first x in the "loaded cards" array 
    -(void)loadCards 
     { 




if([exampleCardLabels count] > 0) { 
    NSInteger numLoadedCardsCap =(([exampleCardLabels count] > MAX_BUFFER_SIZE)?MAX_BUFFER_SIZE:[exampleCardLabels count]); 
    //%%% if the buffer size is greater than the data size, there will be an array error, so this makes sure that doesn't happen 



    //%%% loops through the exampleCardsLabels array to create a card for each label. This should be customized by removing "exampleCardLabels" with your own array of data 
    for (int i = 0; i<[exampleCardLabels count]; i++) { 
     DraggableView* newCard = [self createDraggableViewWithDataAtIndex:i]; 
     [allCards addObject:newCard]; 

     if (i<numLoadedCardsCap) { 
      //%%% adds a small number of cards to be loaded 
      [loadedCards addObject:newCard]; 
     } 
    } 



    //%%% displays the small number of loaded cards dictated by MAX_BUFFER_SIZE so that not all the cards 
    // are showing at once and clogging a ton of data 
    for (int i = 0; i<[loadedCards count]; i++) { 
     if (i>0) { 
      [self insertSubview:[loadedCards objectAtIndex:i] belowSubview:[loadedCards objectAtIndex:i-1]]; 
     } else { 
      [self addSubview:[loadedCards objectAtIndex:i]]; 
     } 
     cardsLoadedIndex++; //%%% we loaded a card into loaded cards, so we have to increment 
    } 
}else if ([exampleCardLabels count] == 0){ 

    [self removeFromSuperview]; 
    //[DraggableViewBackground.self removeFromSuperview]; 
    } 
    } 

    #warning include own action here! 
    //%%% action called when the card goes to the left. 
    // This should be customized with your own action 
    -(void)cardSwipedLeft:(UIView *)card; 
    { 
//do whatever you want with the card that was swiped 
// DraggableView *c = (DraggableView *)card; 

    [loadedCards removeObjectAtIndex:0]; //%%% card was swiped, so it's no  longer a "loaded card" 

     if (cardsLoadedIndex < [allCards count]) { //%%% if we haven't reached the end of all cards, put another into the loaded cards 
    [loadedCards addObject:[allCards objectAtIndex:cardsLoadedIndex]]; 
    cardsLoadedIndex++;//%%% loaded a card, so have to increment count 
    [self insertSubview:[loadedCards objectAtIndex:(MAX_BUFFER_SIZE-1)] belowSubview:[loadedCards objectAtIndex:(MAX_BUFFER_SIZE-2)]]; 
} 
    } 

     #warning include own action here! 
    //%%% action called when the card goes to the right. 
    // This should be customized with your own action 
    -(void)cardSwipedRight:(UIView *)card 
    { 
//do whatever you want with the card that was swiped 
// DraggableView *c = (DraggableView *)card; 

    [loadedCards removeObjectAtIndex:0]; //%%% card was swiped, so it's no longer a "loaded card" 

    if (cardsLoadedIndex < [allCards count]) { //%%% if we haven't reached the end of all cards, put another into the loaded cards 
    [loadedCards addObject:[allCards objectAtIndex:cardsLoadedIndex]]; 
    cardsLoadedIndex++;//%%% loaded a card, so have to increment count 
    [self insertSubview:[loadedCards objectAtIndex:(MAX_BUFFER_SIZE-1)] belowSubview:[loadedCards objectAtIndex:(MAX_BUFFER_SIZE-2)]]; 
} 

    } 

    //%%% when you hit the right button, this is called and substitutes the swipe 
    -(void)swipeRight 
     { 
DraggableView *dragView = [loadedCards firstObject]; 
dragView.overlayView.mode = GGOverlayViewModeRight; 
[UIView animateWithDuration:0.2 animations:^{ 
    dragView.overlayView.alpha = 1; 
}]; 
[dragView rightClickAction]; 
    } 

    //%%% when you hit the left button, this is called and substitutes the swipe 
    -(void)swipeLeft 
    { 
DraggableView *dragView = [loadedCards firstObject]; 
dragView.overlayView.mode = GGOverlayViewModeLeft; 
[UIView animateWithDuration:0.2 animations:^{ 
    dragView.overlayView.alpha = 1; 
}]; 
[dragView leftClickAction]; 
    } 

    -(void)swipeIgnore 
{ 
DraggableView *dragView = [loadedCards firstObject]; 
dragView.overlayView.mode = GGOverlayViewModeLeft; 
[UIView animateWithDuration:0.2 animations:^{ 
    dragView.overlayView.alpha = 1; 
}]; 
[dragView leftClickAction]; 


    } 

    /* 
    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    - (void)drawRect:(CGRect)rect 
    { 
    // Drawing code 
    } 
    */ 

@end

答えて

3

あなたが変更することはできます - また、この方法を変更

を提出し、- (id)initWithFrame:(CGRect)frame方法

から[self loadCards];コメントDraggableViewBackground.hに-(void)loadCards :(NSArray*) array;を追加this-

-(void)loadCards :(NSArray*) array { 
    exampleCardLabels = array; 

    //put here your existing code. 
    //... 
} 

のような(無効)loadCards方法をこのように -

if ([_arrAllCards count] >0) { 

         DraggableViewBackground *draggableBackground = [[DraggableViewBackground alloc]initWithFrame:self.invitationViewContainer.frame]; 

         [draggableBackground loadCards:_arrAllCards]; 
         draggableBackground.alpha = 0; //opti 
         [self.invitationViewContainer addSubview:draggableBackground]; 

         //[draggableBackground setNeedsDisplay]; 
        } 
+0

私はこのexampleCardLabelsを削除する必要がありますか?[NSArray new]; DraggableViewBackgroundビュークラスから、0要素を取得するためです。 – Nivesh

+0

NSLog(@ "%@"、配列)を追加できますか? to - (void)loadCards:(NSArray *)配列をチェックします。要素の –

+0

今、配列を取得していただきありがとうございますが、私はループをビュークラスで終了した後、どのように私はそれをviewcontrollerにリダイレクトできますか?任意のヘルプ – Nivesh

0

私のビューコントローラ

[self.arrAllCards addObject:model]; 
        //[self.arrExamplecards addObject:model.senderImage]; 

        NSLog(@"%@",_arrAllCards); 

        //_draggableVC.allCards = _arrAllCards; 


        //[_draggableVC.exampleCardLabels isEqualToArray:_arrInitationsMainContainer]; 
        if ([_arrAllCards count] >0) { 

         DraggableViewBackground *draggableBackground = [[DraggableViewBackground alloc]initWithFrame:self.invitationViewContainer.frame]; 


         draggableBackground.exampleCardLabels = [NSMutableArray arrayWithArray:_arrAllCards]; 
         draggableBackground.alpha = 0; //opti 
         [self.invitationViewContainer addSubview:draggableBackground]; 

         [draggableBackground setNeedsDisplay]; 
        } 

とDraggableViewBackgroundで私の配列ですされた変更

exampleCardLabels = [NSArray new]; //%%% placeholder for card-specific information

MyClass *myClass = [[MyClass alloc]init]; 
NSMutableArray *array = [myClass.arrAllCards mutableCopy]; 
exampleCardLabels = array 

への注意:MyClassののreferrsあなたのビューコントローラの名前何でもします。

+0

どのようにviewControllerから配列を渡すことができますか?arrAllcardsを意味しますか? – Nivesh

+0

View Controllerで配列arrAllCardを作成している場合(おそらくviewDidLoadで)、上記のコードはView ControllerからDraggableViewBackgroundクラスに配列を渡します。 – Sam

+0

ビューで呼び出されていないので、viewcontrollerで配列を合成する必要がありますか? – Nivesh

関連する問題