0

您好,我在我的应用程序中使用轮播来显示推文。虽然这个过程花费了很多时间来显示视图控制器,所以我使用 GDC 来加载视图控制器,然后在下载推文时更新轮播视图

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if (!view)
    {
        //load new item view instance from nib
        //control events are bound to view controller in nib file
        view = [[[NSBundle mainBundle] loadNibNamed:@"Empty" owner:self options:nil] lastObject];

        //[self performSelectorInBackground:@selector(loadthetweets) withObject:nil ];
        //[self performSelectorInBackground:@selector(loadfromtheweb:) withObject:nil];
        //[self performSelectorOnMainThread:@selector(loadthetweets) withObject:nil waitUntilDone:YES];


        //[self performSelectorInBackground:@selector(loadthetweets) withObject:nil];
        /*
        NSString *feedname=@"http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=shoplog1&count=20";

        NSURL *feedURL =[NSURL URLWithString:feedname];

        //The JSON Part
        NSData *data =[NSData dataWithContentsOfURL:feedURL];

        NSError* error;
        _tweets= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves
                                                   error:&error];



        maintext=[[_tweets objectAtIndex:index]objectForKey:@"text"];
        NSArray *piecesOfOriginalString = [maintext componentsSeparatedByString:@"http"];
        NSString *firstetxt=[piecesOfOriginalString objectAtIndex:0];
        NSLog(@"The first text: %@",firstetxt);
        NSString *link=[@"http" stringByAppendingString:[piecesOfOriginalString objectAtIndex:1]];
        linktogo=[NSURL URLWithString:link];
        text.text=firstetxt;

        NSLog(@"The link text: %@",link);
        //NSDictionary *new =[[[tweets objectAtIndex:index]objectForKey:@"entities"]objectForKey:@"urls"];
        NSDictionary *retweeted=[[_tweets objectAtIndex:index]objectForKey:@"retweeted_status"];
        if (retweeted) {
            NSString *urlll=[[[[_tweets objectAtIndex:index]objectForKey:@"retweeted_status"]objectForKey:@"user"]objectForKey:@"profile_image_url"];
            NSURL *iiii=[NSURL URLWithString:urlll];
            usericon.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:iiii]];
            username.text=[[[[_tweets objectAtIndex:index]objectForKey:@"retweeted_status"]objectForKey:@"user"]objectForKey:@"screen_name"];
        }else{
            username.text=@"Shoplog";
            usericon.image=[UIImage imageNamed:@"MyIcon copy_57.png"];
        }
         */
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        [Provideractivity startAnimating];
        [ContentActivity startAnimating];

        dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        dispatch_async(concurrentQueue, ^{

            [self loadthetweets];
            //[self loadfromtheweb:[NSNumber numberWithUnsignedInt:index ]];
            dispatch_async(dispatch_get_main_queue(), ^{
                //[self loadfromtheweb:[NSNumber numberWithUnsignedInt:index ]];
                maintext=[[_tweets objectAtIndex:index]objectForKey:@"text"];
                NSArray *piecesOfOriginalString = [maintext componentsSeparatedByString:@"http"];
                NSString *firstetxt=[piecesOfOriginalString objectAtIndex:0];
                NSLog(@"The first text: %@",firstetxt);
                NSString *link=[@"http" stringByAppendingString:[piecesOfOriginalString objectAtIndex:1]];
                linktogo=[NSURL URLWithString:link];
                text.text=firstetxt;

                NSLog(@"The link text: %@",link);
                //NSDictionary *new =[[[tweets objectAtIndex:index]objectForKey:@"entities"]objectForKey:@"urls"];
                NSDictionary *retweeted=[[_tweets objectAtIndex:index]objectForKey:@"retweeted_status"];
                if (retweeted) {
                    NSString *urlll=[[[[_tweets objectAtIndex:index]objectForKey:@"retweeted_status"]objectForKey:@"user"]objectForKey:@"profile_image_url"];
                    username.text=[[[[_tweets objectAtIndex:index]objectForKey:@"retweeted_status"]objectForKey:@"user"]objectForKey:@"screen_name"];
                    NSURL *iiii=[NSURL URLWithString:urlll];
                    usericon.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:iiii]];

                }else{
                    username.text=@"Shoplog";
                    usericon.image=[UIImage imageNamed:@"MyIcon copy_57.png"];
                }

                [ContentActivity stopAnimating];
                [ContentActivity hidesWhenStopped];
                [Provideractivity stopAnimating];
                [Provideractivity hidesWhenStopped];



                 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            });
        });



    }
    return view;

}

这里的问题是 Carousel 的第 11 个元素已正确更新,其余部分为空白,我不知道为什么。

有人可以帮忙吗?

4

2 回答 2

0

在您的if (!view) {...}块中,仅创建视图,不要配置它。在此 if 语句之后和之外配置视图。传入的视图- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view可能已经创建,如果是,它不会进入您的 if 块,并且推文将永远不会被加载,因为 if 永远不会被配置。

于 2013-03-29T15:50:01.217 回答
0

感谢您的回答 !,虽然我已经设法将推文的下载放在一个函数之外, (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 并从视图控制器的 initfromnib 函数中调用它,但现在需要更少的时间!

于 2013-05-01T07:58:07.273 回答