gpt4 book ai didi

ios - 如何通过 iOS 中的 objective-c 停止函数递归(在 collectionView 中)

转载 作者:行者123 更新时间:2023-11-28 21:25:42 25 4
gpt4 key购买 nike

编辑:我承认我的设计有问题。只要把我的计时器移到一个非UI相关的功能上,问题就解决了。

首先,我有多个帐户。我有一个 collectionView 来加载一个帐户的信息。

其次,我有两个功能。一个是我的 collectionView 的 cellForItemAtIndexPath 函数。我有一个定时器可以调用另一个函数来更新记录的帐户数据。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

//updateUI methods..

if (// a rare situation ){
dispatch_async(dispatch_get_main_queue(), ^{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:@selector(refreshSingleVMStatusForTimer:)
userInfo:nil repeats:NO];
});
}
}

这里是 refreshSingleVMStatusForTimer 函数,用于从互联网下载所有数据,然后调用 [collectionView reloadData] 来更新它。

- (void) refreshSingleVMStatusForTimer :(NSTimer *) timer {

//download the data from internet
//parse the xml to useful data.

[collectionView reloadData];
}

所以这两个函数就变成了递归。

现在,如果我切换帐户,我的 collectionView 将在其中加载不同帐户的数据。但是递归还在运行,会出现错误。

不知道我把问题说清楚了没有。如果您有任何问题,请告诉我。

最佳答案

正如 Avi 所提到的,这是一个设计问题,它不会让您停止递归:您不应在 -cellForItem:atIndexPath: 中触发重新加载。这是为园丁做的。去掉它。

如果有理由在那里这样做,请将其隔离并提出新问题。

关于ios - 如何通过 iOS 中的 objective-c 停止函数递归(在 collectionView 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38196327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com