Objective c TableView没有';不显示图像

Objective c TableView没有';不显示图像,objective-c,uitableview,twitter,cell,Objective C,Uitableview,Twitter,Cell,我有一个显示推特帐号提要的应用程序。所以我有ImageView、textLabel和detailLabel作为提要的内容。问题是当加载所有数据时,uiimage不会出现。单击单元格或上下滚动时,图像已设置。这是我的一些代码 -(void)getImageFromUrl:(NSString*)imageUrl asynchronouslyForImageView:(UIImageView*)imageView andKey:(NSString*)key{ dispatch_async(dispa

我有一个显示推特帐号提要的应用程序。所以我有ImageView、textLabel和detailLabel作为提要的内容。问题是当加载所有数据时,uiimage不会出现。单击单元格或上下滚动时,图像已设置。这是我的一些代码

-(void)getImageFromUrl:(NSString*)imageUrl asynchronouslyForImageView:(UIImageView*)imageView andKey:(NSString*)key{

dispatch_async(dispatch_get_global_queue(
                                         DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    NSURL *url = [NSURL URLWithString:imageUrl];

    __block NSData *imageData;

    dispatch_sync(dispatch_get_global_queue(
                                            DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        imageData =[NSData dataWithContentsOfURL:url];

        if(imageData){

            [self.imagesDictionary setObject:[UIImage imageWithData:imageData] forKey:key];

            dispatch_sync(dispatch_get_main_queue(), ^{
                imageView.image = self.imagesDictionary[key];
            });
        }
    });

});

}


- (void)refreshTwitterHomeFeedWithCompletion {
    // Request access to the Twitter accounts
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){
        if (granted) {

            NSArray *accounts = [accountStore accountsWithAccountType:accountType];

            // Check if the users has setup at least one Twitter account

            if (accounts.count > 0)
            {
                ACAccount *twitterAccount = [accounts objectAtIndex:0];

                NSLog(@"request.account ...%@",twitterAccount.username);


                NSURL* url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/home_timeline.json"];
                NSDictionary* params = @{@"count" : @"50", @"screen_name" : twitterAccount.username};

                SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                        requestMethod:SLRequestMethodGET
                                                                  URL:url parameters:params];

                request.account = twitterAccount;

                [request performRequestWithHandler:^(NSData *responseData,
                                                     NSHTTPURLResponse *urlResponse, NSError *error) {


                    if (error)
                    {
                        NSString* errorMessage = [NSString stringWithFormat:@"There was an error reading your Twitter feed. %@",
                                                  [error localizedDescription]];
                        NSLog(@"%@",errorMessage);

                    }
                    else
                    {
                        NSError *jsonError;
                        NSArray *responseJSON = [NSJSONSerialization
                                                 JSONObjectWithData:responseData
                                                 options:NSJSONReadingAllowFragments
                                                 error:&jsonError];

                        if (jsonError)
                        {
                            NSString* errorMessage = [NSString stringWithFormat:@"There was an error reading your Twitter feed. %@",
                                                      [jsonError localizedDescription]];
                            NSLog(@"%@",errorMessage);

                        }
                        else
                        {
                            NSLog(@"Home responseJSON..%@",(NSDictionary*)responseJSON.description);
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [self reloadData:responseJSON];

                            });
                        }
                    }
                }];
            }
        }
    }];
}


-(void)reloadData:(NSArray*)jsonResponse
{
    self.tweets = jsonResponse;
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return self.tweets.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    SNTwitterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(!cell)
    {
        cell = [[SNTwitterCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *tweetDictionary = self.tweets[indexPath.row];
    NSDictionary *user = tweetDictionary[@"user"];


    NSString *userName = user[@"name"];
    NSString *tweetContaint = tweetDictionary[@"text"];



    NSString* imageUrl = [user objectForKey:@"profile_image_url"];
    [self getImageFromUrl:imageUrl asynchronouslyForImageView:cell.imageView andKey:userName];

    cell.profileImage.image = [UIImage imageNamed:@"images.png"];

    NSArray *days = [NSArray arrayWithObjects:@"Mon ", @"Tue ", @"Wed ", @"Thu ", @"Fri ", @"Sat ", @"Sun ", nil];
    NSArray *calendarMonths = [NSArray arrayWithObjects:@"Jan", @"Feb", @"Mar",@"Apr", @"May", @"Jun", @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec", nil];
    NSString *dateStr = [tweetDictionary objectForKey:@"created_at"];

    for (NSString *day in days) {
        if ([dateStr rangeOfString:day].location == 0) {
            dateStr = [dateStr stringByReplacingOccurrencesOfString:day withString:@""];
            break;
        }
    }

    NSArray *dateArray = [dateStr componentsSeparatedByString:@" "];
    NSArray *hourArray = [[dateArray objectAtIndex:2] componentsSeparatedByString:@":"];
    NSDateComponents *components = [[NSDateComponents alloc] init];

    NSString *aux = [dateArray objectAtIndex:0];
    int month = 0;
    for (NSString *m in calendarMonths) {
        month++;
        if ([m isEqualToString:aux]) {
            break;
        }
    }
    components.month = month;
    components.day = [[dateArray objectAtIndex:1] intValue];
    components.hour = [[hourArray objectAtIndex:0] intValue];
    components.minute = [[hourArray objectAtIndex:1] intValue];
    components.second = [[hourArray objectAtIndex:2] intValue];
    components.year = [[dateArray objectAtIndex:4] intValue];

    NSTimeZone *gmt = [NSTimeZone timeZoneForSecondsFromGMT:2];
    [components setTimeZone:gmt];


    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [calendar setTimeZone:[NSTimeZone systemTimeZone]];
    NSDate *date = [calendar dateFromComponents:components];

    NSString *tweetDate = [self getTimeAsString:date];

    NSString *tweetValues = [NSString stringWithFormat:@"%@ :%@",userName,tweetDate];




    cell.textLabel.text = [NSString stringWithFormat:@"%@",tweetValues];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",tweetContaint];

    [cell.detailTextLabel setFont:[UIFont fontWithName:@"Helvetica" size:20]];



    return cell;
}

- (NSString*)getTimeAsString:(NSDate *)lastDate {
    NSTimeInterval dateDiff =  [[NSDate date] timeIntervalSinceDate:lastDate];

    int nrSeconds = dateDiff;//components.second;
    int nrMinutes = nrSeconds / 60;
    int nrHours = nrSeconds / 3600;
    int nrDays = dateDiff / 86400; //components.day;

    NSString *time;
    if (nrDays > 5){
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateStyle:NSDateFormatterShortStyle];
        [dateFormat setTimeStyle:NSDateFormatterNoStyle];

        time = [NSString stringWithFormat:@"%@", [dateFormat stringFromDate:lastDate]];
    } else {
        // days=1-5
        if (nrDays > 0) {
            if (nrDays == 1) {
                time = @"1 day ago";
            } else {
                time = [NSString stringWithFormat:@"%d days ago", nrDays];
            }
        } else {
            if (nrHours == 0) {
                if (nrMinutes < 2) {
                    time = @"just now";
                } else {
                    time = [NSString stringWithFormat:@"%d minutes ago", nrMinutes];
                }
            } else { // days=0 hours!=0
                if (nrHours == 1) {
                    time = @"1 hour ago";
                } else {
                    time = [NSString stringWithFormat:@"%d hours ago", nrHours];
                }
            }
        }
    }

    return [NSString stringWithFormat:NSLocalizedString(@"%@", @"label"), time];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(void)getImageFromUrl:(NSString*)imageUrl异步用于imageView:(UIImageView*)imageView和key:(NSString*)键{
调度异步(调度获取全局队列)(
调度队列优先级默认值为0)^{
NSURL*url=[NSURL URLWithString:imageUrl];
__块NSData*图像数据;
调度同步(调度获取全局队列)(
调度队列优先级默认值为0)^{
imageData=[NSData datawithcontentsofull:url];
if(图像数据){
[self.imagesDictionary setObject:[UIImage imageWithData:imageData]forKey:key];
调度同步(调度获取主队列()^{
imageView.image=self.imagesDictionary[key];
});
}
});
});
}
-(无效)刷新TwitterHomeFeedWithCompletion{
//请求访问Twitter帐户
ACAccountStore*accountStore=[[ACAccountStore alloc]init];
ACAccountType*accountType=[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierWitter];
[accountStore requestAccessToAccountsWithType:accountType选项:无完成:^(已授予BOOL,n错误*错误){
如果(授予){
NSArray*accounts=[accountStore accountsWithAccountType:accountType];
//检查用户是否至少设置了一个Twitter帐户
如果(accounts.count>0)
{
ACAccount*twitterAccount=[accounts objectAtIndex:0];
NSLog(@“request.account…%@”,twitterAccount.username);
NSURL*url=[NSURL URLWithString:@”https://api.twitter.com/1.1/statuses/home_timeline.json"];
NSDictionary*params=@{@“count”:@“50”,@“screen_name”:twitterAccount.username};
SLRequest*request=[SLRequestRequestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:URL参数:params];
request.account=twitterAccount;
[请求执行请求处理程序:^(NSData*responseData,
NSHTTPURLResponse*urlResponse,NSError*error){
如果(错误)
{
NSString*errorMessage=[NSString stringWithFormat:@“读取您的Twitter提要时出错。%@”,
[错误本地化描述]];
NSLog(@“%@”,错误消息);
}
其他的
{
n错误*jsonError;
NSArray*responseJSON=[NSJSONSerialization
JSONObjectWithData:responseData
选项:NSJSONReadingAllowFragments
错误:&jsonError];
if(jsonError)
{
NSString*errorMessage=[NSString stringWithFormat:@“读取您的Twitter提要时出错。%@”,
[jsonError本地化描述]];
NSLog(@“%@”,错误消息);
}
其他的
{
NSLog(@“Home responseJSON..%@),(NSDictionary*)responseJSON.description);
dispatch\u async(dispatch\u get\u main\u queue()^{
[自重新加载数据:responseJSON];
});
}
}
}];
}
}
}];
}
-(void)重新加载数据:(NSArray*)jsonResponse
{
self.tweets=jsonResponse;
[self.tableView重载数据];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//返回节中的行数。
返回self.tweets.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
SNTwitterCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(!单元格)
{
cell=[[SNTwitterCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}
NSDictionary*tweetDictionary=self.tweets[indexPath.row];
NSDictionary*user=tweetDictionary[@“user”];
NSString*userName=user[@“name”];
NSString*tweetContaint=tweetDictionary[@“text”];
NSString*imageUrl=[user objectForKey:@“profile_image_url]”;
[self-getImageFromUrl:imageUrl异步解析imageView:cell.imageView和key:userName];
cell.profileImage.image=[UIImage ImageName:@“images.png”];
NSArray*天=[NSArray阵列,其对象为:周一、周二、周三、周四、周五、周六、周日、零];
NSArray*日历月=[NSArray阵列,其对象为:一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月、零];
NSString*dateStr=[tweetDictionary objectForKey:@“创建于”];
用于(NSString*天,以天为单位){
如果([dateStr rangeOfString:day]。位置==0){
dateStr=[DateStringByReplacingOccurrences