UICollectionViewCell
にはUILabel
があり、2つの日付間の日差を示しています。私はそれを毎秒更新したい。私はUICollectionview
のデータをリロードするのにNSTimer
を使用しようとしましたが、UIが非常に遅くなります。毎秒UICollectionViewCellのUILabelを更新するには
これを達成するにはどうすればよいですか?おかげさまで
UICollectionViewCell
にはUILabel
があり、2つの日付間の日差を示しています。私はそれを毎秒更新したい。私はUICollectionview
のデータをリロードするのにNSTimer
を使用しようとしましたが、UIが非常に遅くなります。毎秒UICollectionViewCellのUILabelを更新するには
これを達成するにはどうすればよいですか?おかげさまで
ラベルを更新するために、CollectionView全体または個々のセルさえも再ロードしません。
あなたは2つのオプションがあります:
タイマーがトリガーされたら、次の方法で可視セルを取得し、時刻を表示するラベルだけを取得します。
let visibleIndexPaths = tableview.indexPathsForVisibleRows
var visibleCellsArray: [UITableViewCell] = []
for currentIndextPath in visibleIndexPaths! {
// You now have visible cells in visibleCellsArray
visibleCellsArray.append(tableview.cellForRowAtIndexPath(currentIndextPath)!)
}
最初に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");
}
に使用して、それが働いているので、同じ私はそれはあなたを助ける願っています私の側では非常によくあなたと同じ作業を希望します。
オプション1のメソッドをviewDidLoadに配置しますか? – Khoury
@KhouryはいviewDidLoadは問題ありません。また、 'viewcontroller'の 'dealloc'メソッドでタイマーを '無効にする'ことを忘れないでください。 – 7vikram7
オプション1を稼働させることができません。可能であれば、オプション2について詳細を教えてください。 – Khoury