2016-07-19 18 views
0

UICollectionViewCellにはUILabelがあり、2つの日付間の日差を示しています。私はそれを毎秒更新したい。私はUICollectionviewのデータをリロードするのにNSTimerを使用しようとしましたが、UIが非常に遅くなります。毎秒UICollectionViewCellのUILabelを更新するには

これを達成するにはどうすればよいですか?おかげさまで

答えて

1

ラベルを更新するために、CollectionView全体または個々のセルさえも再ロードしません。

あなたは2つのオプションがあります:

  1. はViewControllerをでNSTimerを書くだけ可視セルの内容を更新します。 毎秒、ViewControllerで繰り返しタイマーをスケジュールします。

タイマーがトリガーされたら、次の方法で可視セルを取得し、時刻を表示するラベルだけを取得します。

let visibleIndexPaths = tableview.indexPathsForVisibleRows 

    var visibleCellsArray: [UITableViewCell] = [] 

    for currentIndextPath in visibleIndexPaths! { 
     // You now have visible cells in visibleCellsArray 
     visibleCellsArray.append(tableview.cellForRowAtIndexPath(currentIndextPath)!) 
    } 
  • スケジュールの各セルに1秒の繰り返しタイマー。タイマーが起動されたら、それぞれのセルのラベルを更新するだけです。
  • +0

    オプション1のメソッドをviewDidLoadに配置しますか? – Khoury

    +0

    @KhouryはいviewDidLoadは問題ありません。また、 'viewcontroller'の 'dealloc'メソッドでタイマーを '無効にする'ことを忘れないでください。 – 7vikram7

    +0

    オプション1を稼働させることができません。可能であれば、オプション2について詳細を教えてください。 – Khoury

    0

    最初にCollectionView haderに設定し、使いやすいようにhaderラベルとラベルセットをglobalに設定します。タイマー方法を設定し

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { 
    
        if (kind == UICollectionElementKindSectionHeader) { 
    
         Like = indexPath; 
    
         UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; 
    
         if (reusableview==nil) { 
    
          reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; 
         } 
    
         for (UILabel *lbl in reusableview.subviews) 
         { 
          if ([lbl isKindOfClass:[UILabel class]]) 
          { 
           [lbl removeFromSuperview]; 
          } 
         } 
    
         label2=[[UILabel alloc] initWithFrame:CGRectMake(label.frame.size.width + 40, 0, 140, 50)]; 
         [reusableview removeFromSuperview]; 
         label2.text= [NSString stringWithFormat:@"END %@",[self testDateComaparFunc:[[DealArr valueForKey:@"end_time"] objectAtIndex:indexPath.section]]]; 
    
         label2.textColor = [UIColor whiteColor]; 
         [reusableview addSubview:label2]; 
    
         return reusableview; 
        } 
    
        return nil; 
    } 
    

    今viewWillAppearでタイマー設定

    - (void)viewWillAppear:(BOOL)animated 
    { 
        [super viewWillAppear:animated]; 
    
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runMethod) userInfo:nil repeats:YES]; 
    } 
    

    あなたがラベルテキスト

    -(void)runMethod 
    { 
        label2.text= [NSString stringWithFormat:@"END %@",[self testDateComaparFunc:[[DealArr valueForKey:@"end_time"] objectAtIndex:Like.section]]]; 
        NSLog(@"i'm calling"); 
    } 
    

    に使用して、それが働いているので、同じ私はそれはあなたを助ける願っています私の側では非常によくあなたと同じ作業を希望します。

    関連する問題