【问题标题】:Pull To Refresh crashes app if I try to scroll while refreshing如果我在刷新时尝试滚动,则拉到刷新会导致应用程序崩溃
【发布时间】:2015-12-02 14:26:03
【问题描述】:

我已经在我的应用程序上实现了“拉动刷新”,但是当我尝试滚动而它仍在刷新时它会崩溃。这是我正在使用的代码: 仅供参考,我在刷新时正在调用 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;
            });
        });
    }

}

请帮忙!

【问题讨论】:

  • 您的编码是否正确检查是否添加了 self.messageStore 值
  • 谢谢,伙计。我意识到我正在再次初始化数组。我删除了那条线,它现在可以工作了

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


【解决方案1】:

我从未使用过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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2017-07-28
    • 2018-02-07
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多