我正在从Rotten Tomatoes API中获取UILabel
的值,并且我希望我的自定义单元格中的文本在每次获取值时都会更新。我尝试通过重新加载我的UITableView
来做到这一点,但它会循环播放。
这是我的代码:
if (ratingTomatoLabel.text == nil)
{
NSLog(@" Nil");
NSString *stringWithNoSpaces = [movieTitle.text stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString *rottenTomatoRatingString = [NSString stringWithFormat:@"%@%@%@", @"http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=6844abgw34rfjukyyvzbzggz&q=", stringWithNoSpaces, @"&page_limit=1"];
NSLog(@" Rotten URL: %@", rottenTomatoRatingString);
NSURL *rottenUrl = [[NSURL alloc] initWithString:rottenTomatoRatingString];
NSURLRequest *rottenRequest = [[NSURLRequest alloc] initWithURL:rottenUrl];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:rottenRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@" 3");
self.testDict = [JSON objectForKey:@"movies"];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
NSLog(@" %@", ratingTomatoLabel.text);
ratingTomatoLabel.text = @"...";
}
else if ([ratingTomatoLabel.text isEqualToString:@""])
{
NSLog(@" isEqualToString");
// Do nothing
}
else
{
NSLog(@" else");
}
return tableCell;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@" fetched!");
if ([ratingTomatoLabel.text isEqualToString:@"..."])
{
NSLog(@" 2nd nil");
NSDictionary *dict = [self.testDict valueForKey:@"ratings"];
self.criticsScoreString = [NSString stringWithFormat:@"%@", [dict valueForKey:@"critics_score"]];
self.criticsScoreString = [self.criticsScoreString stringByReplacingOccurrencesOfString:@" " withString:@""];
self.criticsScoreString = [self.criticsScoreString stringByReplacingOccurrencesOfString:@"(" withString:@""];
self.criticsScoreString = [self.criticsScoreString stringByReplacingOccurrencesOfString:@")" withString:@""];
self.criticsScoreString = [NSString stringWithFormat:@"%@%@", self.criticsScoreString, @"%"];
ratingTomatoLabel.text = self.criticsScoreString;
NSLog(@" %@", ratingTomatoLabel.text);
}
else
{
NSLog(@" 2nd not nil");
NSLog(@" %@", ratingTomatoLabel.text);
// Do nothing
}
}
如果我在设置文本中的值后添加了代码[self.myTableView reloadData];
,则会循环播放。最好的方法是什么?谢谢!
更新:包含更新的代码和其他方法。另外,我的ratingTomatoLabel在没有显示时总是为零。
答案 0 :(得分:0)
Jaytrix
AFNetworking调用是异步发生的,考虑使用某种通知来告诉主线程(UI)何时完成工作或发生错误。
谢
答案 1 :(得分:0)
您应该使用KVO (Key-value observing)来检测字典或数组何时发生更改 - 在这种情况下似乎self.testDict
。
当您收到此通知时,请使用UITableView的-visibleCells
方法仅更新可见单元格 - 其他人显然会在使用-tableView:cellForRowAtIndexPath:
回收其单元格时使用其新值。
KVO需要什么:在视图加载/更改时注册/取消注册并设置您想要观察的变量......
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqual:@"testDict"])
{
NSLog(@"KVO change: %@", change);
NSArray *visibleCells = [self.myTableView visibleCells];
for(UITableViewCell *cell in visibleCells)
{
if(/*cell matches the one I'm targeting OR just update regardless*/)
// assign value to UILabel
}
}
}
// Call this from -viewDidLoad
- (void)registerAsObserver
{
[self addObserver:self forKeyPath:@"testDict" options:NSKeyValueObservingOptionNew context:NULL];
}
// Call this from -viewWillDisappear and -viewDidUnload (may not be necessary for -viewDidUnload if already implemented in -viewWillDisappear)
- (void)unregisterAsObserver
{
[self removeObserver:self forKeyPath:@"testDict"];
}
最后,要更新已更改的单元格,请调用-reloadRowsAtIndexPaths:withRowAnimation:
方法让底层系统刷新单元格。