使用代码块异步加载UILabel

时间:2014-12-09 08:42:09

标签: ios xcode asynchronous uilabel codeblocks

我遇到了如何异步加载几个UILabel的问题。

这是我的cellForRowAtIndexPath方法:

更新:

根据以下答案,我做了更改:

这是我的新cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Retrieve cell
NSString *cellIdentifier = @"BuildingItem";
BuildingsTableViewCell *cell = [[tableView dequeueReusableCellWithIdentifier:cellIdentifier] initWithFrame:CGRectMake(10, 10,580, 100)];
// Get the area to be shown

Buildings *item = _feedItems[indexPath.row];

NSURL *MyURL = [NSURL URLWithString:item.buildingPointerImage];
UIImage *placeholder = [UIImage imageNamed:@"placeholder"];
NSString *path = [MyURL absoluteString];
NSString *key = [path MD5Hash];
[cell.buildingImageView loadImageFromURL:(NSURL*)MyURL placeholderImage:(UIImage*)placeholder cachingKey:(NSString*)key];
cell.buildingName.text = item.buildingPointerName;
cell.buildingYear.text = item.buildingPointerYear;
NSString *URL = @"http://rets.miamiresidential.com/ios/condos/buildings.php?action=get_range";
NSString *streetNumberURL = [NSString stringWithFormat:@"&street_number=%@",item.buildingStreetNumber];
NSString *streetNameURL = [NSString stringWithFormat:@"&street_name=%@",item.buildingStreetName];
NSString *ZipcodeURL = [NSString stringWithFormat:@"&zipcode=%@",item.buildingZipcode];
NSString *P1 = [URL stringByAppendingString:streetNumberURL];
NSString *P2 = [P1 stringByAppendingString:streetNameURL];
NSString *P3 = [P2 stringByAppendingString:ZipcodeURL];
NSURL *jsonFileUrl = [NSURL URLWithString:P3];
NSLog(@"%@",P3);
[cell.buildingSalesRange loadSalesRangeFromURL:(NSURL*)jsonFileUrl];
[cell.buildingRentalsRange loadRentalsRangeFromURL:(NSURL*)jsonFileUrl];
cell.layer.borderColor = [[UIColor whiteColor]CGColor];
cell.layer.backgroundColor = [[UIColor clearColor]CGColor];
cell.layer.borderWidth = 2;

return cell;
}

这是我的新.m文件:

#import "PriceRanges.h"
#import <objc/runtime.h>

@implementation UILabel(Prices)


-(void) loadSalesRangeFromURL:(NSURL*)url {

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyStyle setLocale:locale];
[currencyStyle setMaximumFractionDigits:0];

NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     if (error)
     {
         NSLog(@"Error,%@", [error localizedDescription]);
     }
     else
     {
         NSArray *priceArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
         NSNumber *number = [priceArray valueForKeyPath:@"sales.number"];
         NSString *lowest = [priceArray valueForKeyPath:@"sales.lowest"];
         NSDecimalNumber *lowestDecimal = [NSDecimalNumber decimalNumberWithString:lowest];
         NSString *lowestPrice = [currencyStyle stringFromNumber:lowestDecimal];
         if (![number isEqualToNumber:[NSNumber numberWithInt:0]]) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 UILabel *labelFromData = [[UILabel alloc] init];
                 [labelFromData setText:[NSString stringWithFormat:@"%@ for Sale from %@",number,lowestPrice]];
                 if (labelFromData) {
                     if ([self.text isEqualToString:labelFromData.text]) {
                     } else {
                         dispatch_async(dispatch_get_main_queue(), ^{
                             self.text = labelFromData.text;
                         });
                     }
                 }
                 self.text = [NSString stringWithFormat:@"%@ for Sale from %@",number,lowestPrice];
            });
         }
 };
 }];
}

-(void) loadRentalsRangeFromURL:(NSURL*)url {

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyStyle setLocale:locale];
[currencyStyle setMaximumFractionDigits:0];

NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     if (error)
     {
         NSLog(@"Error,%@", [error localizedDescription]);
     }
     else
     {
         NSArray *priceArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
         NSNumber *number = [priceArray valueForKeyPath:@"rentals.number"];
         NSString *lowest = [priceArray valueForKeyPath:@"rentals.lowest"];
         NSDecimalNumber *lowestDecimal = [NSDecimalNumber decimalNumberWithString:lowest];
         NSString *lowestPrice = [currencyStyle stringFromNumber:lowestDecimal];
         if (![number isEqualToNumber:[NSNumber numberWithInt:0]]) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self setText:[NSString stringWithFormat:@"%@ for Rent from %@",number,lowestPrice]];
             });
         }
     }
 }];
}

@end

正如您可能看到的,我有两种不同的方法,但每次滚动表时,两者仍然会发送异步请求。

我想我现在还不清楚该做什么?

1 个答案:

答案 0 :(得分:0)

没有独立于您的UI的数据模型的问题,以及模型 - 视图 - 控制器何时有意义的一个很好的例子。您需要另一层(最好是一个单独的类),负责保存您从网络中获取的所有数据,确定它的年龄足以丢弃等等。单元格应该从模型保留的缓存数据,如果数据尚未存在,模型将异步提取数据,然后在新数据到达时通知View Controller。如果单元格仍然可见,则会填充它们。如果他们已经在屏幕外滚动,重复使用等,那么数据更新不会导致任何即时的UI更改。