如果我在刷新时尝试滚动,则“拉到刷新”会崩溃应用程序

时间:2015-09-07 05:39:29

标签: ios objective-c pull-to-refresh

我已经在我的应用上实现了Pull to Refresh,但是当我尝试滚动时它仍然会刷新时崩溃。这是我正在使用的代码: 仅供参考,我正在刷新时调用webServices。

在ViewDidLoad中:

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
    [refresh addTarget:self action:@selector(callWebService:) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refresh;
    [self callWebService:refresh];

刷新方法:

-(void)callWebService:(UIRefreshControl *)refresh
{
    static BOOL refreshInProgress = NO;
    MessageWebServices *messageWebService = [[MessageWebServices alloc]init];

    MessageBO *tempBO = [[MessageBO alloc]init];

    if(self.flag)
    {
        self.navigationItem.title = @"My Posts";
        tempBO.projectId = [AppConstants getProjectId];
        tempBO.customerId =[AppConstants getCustomerId];

    }
    else
    {
        tempBO.projectId = [AppConstants getProjectId];
    }


    self.messageStore = [[NSMutableArray alloc] init];

    if (!refreshInProgress)
    {
        refreshInProgress = YES;

        refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing"];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            self.messageStore = [messageWebService getMessageList:tempBO];

            dispatch_async(dispatch_get_main_queue(), ^{
                [refresh beginRefreshing];
                [self.tableView reloadData];
                [refresh endRefreshing];
                refreshInProgress = NO;
            });
        });
    }

}

请帮助!

1 个答案:

答案 0 :(得分:0)

我从未使用beginRefreshing方法。您可以轻松地向tableView添加刷新控件。

#pragma mark Refresh Method

-(void)startRefreshing {
    if (!self.refreshControl) {
        // Initialize the refresh control.
        self.refreshControl = [[UIRefreshControl alloc] init];
        self.refreshControl.backgroundColor = CRREFRESHBGCOLOR;
        self.refreshControl.tintColor = [UIColor whiteColor];
        [self.refreshControl addTarget:self action:@selector(callWebservice) forControlEvents:UIControlEventValueChanged];
        [yourTable addSubview:self.refreshControl];//add refresh control to your table
    }
}

-(void)endRefreshing {
    if (self.refreshControl) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"MMM d, h:mm a"];
        NSString *title = [NSString stringWithFormat:@"Last update: %@", [formatter stringFromDate:[NSDate date]]];
        NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                                                    forKey:NSForegroundColorAttributeName];
        NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
        self.refreshControl.attributedTitle = attributedTitle;

        [self.refreshControl endRefreshing];
    }
}

调用/完成网络服务方法调用[self endRefreshing];方法并重新加载表格[yourTable reloadData]后。