刷卡中的数组获取第一个元素

时间:2016-12-07 10:25:07

标签: ios objective-c arrays string

我在控制器中创建了一个数组。基于该JSON数组,我的卡[tinderswipecards / cwRichard]被创建。现在我想要,如果我删除一张卡片,我想获取卡片的数据显示在我的viewController中的某个地方显示如下like this

我的卡来自DraggableBackgroundView类,我在我自己的viewController中实现它

_peopleNearBypeopleList = [NSMutableArray new];



 [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
                              {

   PeopleNearByIndividualModel * model = [PeopleNearByIndividualModel new];
                     NSDictionary *dicData = obj;
         model.senderId = [[dicData objectForKey:USER_ID] integerValue];
         model.displayName = [[dicData objectForKey:DISPLAY_NAME] uppercaseString];
         model.userImage = [dicData objectForKey:@"image"];
         [self.peopleNearBypeopleList addObject:model];

      NSLog(@"%@",_peopleNearBypeopleList);

      if (idx == [dataArray count] - 1) {

         [self createInvitationViews];
         }

和要创建的字符串,如

 int i;
 for (i = 0; i < [_peopleNearBypeopleList count]; i++) {


  PeopleNearByIndividualModel * model = [_peopleNearBypeopleList objectAtIndex:i];




  NSString *nameString = @"ARE YOU SURE YOU WANT TO INVITE ";



  nameString = [nameString stringByAppendingString:model.displayName];
    NSLog(@"%@", nameString);

    _textView.textColor = [UIColor whiteColor];
    _textView.font = [UIFont fontWithName:@"Roboto-Regular" size:17.0f];
    _textView.text = nameString;

}
再次

它正在调用第一个Object.how我可以实现这个

这是我的draggableViewBackground Class

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

     exampleCardLabels = array;


     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++) {
              MG_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
         }
      } 
//      }
     }

    #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
     {
 MG_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
    {
  MG_DraggableVIew *dragView = [loadedCards firstObject];
  dragView.overlayView.mode = GGOverlayViewModeLeft;
 [UIView animateWithDuration:0.2 animations:^{
    dragView.overlayView.alpha = 1;
  }];
   [dragView leftClickAction];
   }

2 个答案:

答案 0 :(得分:1)

您可以在NSNotification内发布-(void)cardSwipedLeft:(UIView *)card&amp; -(void)cardSwipedRight:(UIView *)card中的DraggableViewBackground方法。

NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:index] forKey:@"someKey"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self userInfo:userInfo];

此处索引是您的卡片索引。要设置索引,可以声明名为index的int属性。在index=0;方法中设置initwithframe等索引的初始值。

然后,在刷卡时增加索引值。

index++;内设置-(void)cardSwipedLeft:(UIView *)card&amp; -(void)cardSwipedRight:(UIView *)card中的DraggableViewBackground方法。

在视图控制器中监听此通知。为此,通过以下行添加viewDidLoad中的视图控制器方法:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveTestNotification:)
                                                 name:@"TestNotification"
                                               object:nil];

在viewcontroller中实现这个选择器方法 -

- (void) receiveTestNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"TestNotification"]){
         NSLog (@"Successfully received the test notification!");
        NSDictionary *userInfo = notification.userInfo;
        int myObject = [[userInfo objectForKey:@"someKey"]integerValue];
        NSLog(@"index: %d", myObject);
    }

}

receiveTestNotification方法上获得卡片索引。那么你可以通过索引从数组中获取数据。

答案 1 :(得分:-1)

在我看来,如果您为卡片及其dictionary保留data,那将会更好。

您可以使用键作为卡ID /卡号和值作为数据创建字典(说明。)

因此,无论何时移除卡片,只需从data获取dictionary

让我们说cardsDictionary是你的dictionary

cardsDictionary = @{@"card1_id":@"card1_data"......@"cardn_id":@"cardn_data"};

然后在取出卡片时你可以这样做:

card_data = cardsDictionary[@"card_id"];