UIRefreshControl at the bottom of the UITableView iOS6?

前端 未结 8 1925
你的背包
你的背包 2020-12-01 06:27

Is it possibile add UIRefreshControl at the bottom of the UITableView? I would use it to load more data. Please, Any suggest?

相关标签:
8条回答
  • 2020-12-01 06:50

    For UITableView I suggest the following approach:

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {    
        // if this is the last row that we have in local data
        // and we didn't reach the total count of rows from the server side
        if (count == indexPath.row+1 && count < total)
        {
            // .. fetch more rows and reload table when data comes
        }
    }
    

    I didn't use the scroll view methods intentionally because the scroll view is slow. It happens that scroll view scrolls, we request more data, refresh more rows, and scroll view is not yet finished with scrolling, it is still decelerating and causing problems with fetching more data.

    0 讨论(0)
  • 2020-12-01 06:55

    I've been stuck on the same problem recently and write a category for UIScrollView class. Here it is CCBottomRefreshControl.

    0 讨论(0)
  • 2020-12-01 06:55

    This Swift library add pull to refresh to the bottom of UITableview.

    https://github.com/marwendoukh/PullUpToRefresh-iOS

    I hope this help you.

    0 讨论(0)
  • 2020-12-01 06:58

    Actually the free Sensible TableView framework does provide this functionality out of the box. You specify the batch number and it will automatically fetch the data, displaying the last cell as a 'load more' cell. Once you click that cell, it will automatically load the next batch, and so on. Worth taking a look at.

    0 讨论(0)
  • 2020-12-01 07:06

    The following answer is in Xcode 8 and Swift 3.

    first declare

    var spinner = UIActivityIndicatorView()
    

    than in viewDidLoad method write the following code:

    spinner = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
    spinner.stopAnimating()
    spinner.hidesWhenStopped = true
    spinner.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 60)
    tableView.tableFooterView = spinner
    

    now finally override the scrollViewDidEndDragging delegate method with following:

    override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
            let offset = scrollView.contentOffset
            let bounds = scrollView.bounds
            let size = scrollView.contentSize
            let inset = scrollView.contentInset
    
            let y = offset.y + bounds.size.height - inset.bottom
            let h = size.height
    
            let reloadDistance = CGFloat(30.0)
            if y > h + reloadDistance {
                print("fetch more data")
                spinner.startAnimating()
            }
    }
    
    0 讨论(0)
  • 2020-12-01 07:08

    You can use UIView to customize your refreshControl at bottom. Create UIView and add it to UITableView as footerView.

    UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    
    [footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"refreshImage.png"]]];
    
    tableView.tableFooterView = footerView;
    

    Hide it: tableView.tableFooterView.hidden = YES;

    Implement UIScrollView delegate -

    (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
        {
                 tableView.tableFooterView.hidden = NO;
                // call method to add data to tableView
        }
    
    }
    

    before adding data to tableView save current offset by CGPoint offset = tableView.contentOffset;

    call reloadData then set previously saved offset back to tableView [tableView setContentOffset:offset animated:NO];

    So that you can feel new data added at bottom.

    0 讨论(0)
提交回复
热议问题