我正在UILabel
从 Rotten Tomatoes API 获取我的值,并且我希望我的自定义单元格中的文本在已经获取值时更新。我尝试通过重新加载我的来做到这一点,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 在不显示时总是为零。