如何使用AFNetworking在UITableCell内部重新加载文本 [英] How to Reload Text Inside UITableCell using AFNetworking
问题描述
我正在从Rotten Tomatoes API中获取我的 UILabel
的值,我希望自定义单元格中的文本只要已经获取该值就可以更新。我尝试通过重新加载我的 UITableView
来做到这一点,但是它循环了。
I'm fetching the value of my UILabel
from Rotten Tomatoes API and I want my text inside my custom cell to update whenever the value has been fetched already. I tried doing that by reloading my UITableView
but it goes on a loop.
这是我的代码:
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];
就会循环运行。最好的方法是什么?谢谢!
If I added the code [self.myTableView reloadData];
after setting the value in my text, it goes on a loop. What's the best way to do this? Thanks!
更新:包括更新的代码和另一种方法。另外,我的ratingTomatoLabel在未显示时始终为零。
UPDATE: Included updated code and another method. Also, my ratingTomatoLabel always goes nil when it's not displayed.
推荐答案
您应使用 KVO(键值观察)来检测字典或数组是否发生更改-在这种情况下,似乎是 self.testDict
。
You should use KVO (Key-value observing) to detect when there are changes to your dictionary or array - in this case it seems self.testDict
.
收到此通知时,请使用UITableView的 -visibleCells
方法仅更新可见的单元格-当其他单元格使用 -tableView:cellForRowAtIndexPath:$ c $回收单元格时,其他单元格显然会采用其新值c>。
When you receive this notification, use UITableView's -visibleCells
method to update only the visible cells - the others will obviously take on their new value when their cell is recycled using -tableView:cellForRowAtIndexPath:
.
KVO需要什么:当视图加载/更改并设置您要观察的变量时注册/注销...
What's required for KVO: Registering/deregistering when views load/change and setting the variable you wish to observe...
- (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:
方法可让底层系统刷新单元格。
Lastly, to update the cell(s) that have changed, call the -reloadRowsAtIndexPaths:withRowAnimation:
method to have the underlying system refresh the cell.
这篇关于如何使用AFNetworking在UITableCell内部重新加载文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!