自定义iOS"加载更多"行列表中的按钮

时间:2014-09-04 22:27:52

标签: ios uitableview parse-platform

我正在使用带有滚动列表的UItableview。现在我有默认的“加载更多”按钮,一次加载10个对象。它破坏了界面。关于如何定制它的任何想法?尝试了一些事情,但我们没有运气。感谢

2 个答案:

答案 0 :(得分:3)

尝试实现Parse提供的用于自定义“加载更多”单元格的方法:

// Override to customize the look of the cell that allows the user to load the next page of objects.
// The default implementation is a UITableViewCellStyleDefault cell with simple labels.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"NextPage";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = @"Load more...";

    return cell;
}

答案 1 :(得分:0)

在自定义方面,你可以用Parse控制器做很多事情。此控制器的动画似乎有点笨重。在使用具有最新IO 9的组件时,我也遇到了一堆内存错误。

出于同样的原因,我用以下内容替换了现有的Parse控制器。让我比标准的PARSE控制器更能控制动画和设计。

如果您想将tableview的顺序颠倒为看起来像信使应用,请查看here

添加全局变量

 var objects = [PFObject(className: "Posts")]
 var lockScrollSensor = false
 let objectsPerPage = 10

添加到viewDidLoad

//blank screen
let blankView = UIView(frame: CGRectMake(0, 0,     self.tableView.frame.size.width, self.tableView.frame.size.height - 60))
blankView.tag = 101
blankView.backgroundColor = customColorYellow()
self.view.addSubview(blankView)
self.tableView.backgroundColor = customColorYellow()
// load tableview
tableViewFirstLoad()

检测tableview滚动的位置,以调用刷新或下一页

 override func scrollViewDidScroll(scrollView: UIScrollView) {

        let scrollViewHeight = scrollView.frame.size.height;
        let scrollContentSizeHeight = scrollView.contentSize.height;
        let scrollOffset = scrollView.contentOffset.y;

        // refresh - touch top
        if (scrollOffset < -60)
        {
            if lockScrollSensor == false {
                // stop method from non stop calling
                lockScrollSensor = true

                // loader
                let spinningActivity = MBProgressHUD.showHUDAddedTo(self.parentViewController?.view, animated: true)
                spinningActivity.color = UIColor.clearColor()
                spinningActivity.activityIndicatorColor = customColorRed()
                spinningActivity.yOffset = -Float((self.parentViewController?.view.frame.height)!/2) + 95

                // call method to load more
                self.tableViewRefresh({ (done) -> Void in
                    if done == true
                    {
                        // stop loader and free method
                        spinningActivity.hide(true, afterDelay: 0)
                        self.lockScrollSensor = false

                        // animate tableview
                        self.tableView.contentOffset.y = -60
                        UIView.animateWithDuration(1.5, delay: 1, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                            self.tableView.contentOffset.y = 0
                                }, completion: nil)
                    }
                })
            }
        }
        // load more -  touch down
        else if (scrollOffset + scrollViewHeight > scrollContentSizeHeight)
        {
            if lockScrollSensor == false {
                // stop method from non stop calling
                lockScrollSensor = true
                // start loader
                let spinningActivity = MBProgressHUD.showHUDAddedTo(self.parentViewController?.view, animated: true)
                spinningActivity.color = customColorYellow()
                spinningActivity.yOffset = (Float(tableView.frame.size.height)/2)-25
                // call method to load more
                self.tableViewLoadMore({ (done) -> Void in
                    if done == true {
                        self.lockScrollSensor = false
                        spinningActivity.hide(true)
                    }
                })
            }
        }
    }

将加载PFObject阵列的刷新或下一页方法。

// first load
func tableViewFirstLoad(result: (done: Bool) -> Void){
    var query = PFQuery(className: "Posts")
    query = segueDataChecker()
    // limit objects
    query.limit = objectsPerPage
    // get objects
    query.findObjectsInBackgroundWithBlock { (posts: [AnyObject]?, error: NSError?) -> Void in
        if error != nil
        {
            result(done: true)
            let alertView = createDefaultAlertError(message: "\(error!.localizedDescription.capitalizedString)", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil)])
            self.presentViewController(alertView, animated: true, completion: nil)
        }
        else
        {
            // clean array
            self.objects.removeAll()
            // populate array
            for eachPost in posts!
            {
                if self.objects.count < self.objectsPerPage
                {
                    self.objects.append(eachPost as! PFObject)
                }
            }
            // reload and send back it has finished
            self.tableView.reloadData()
            result(done: true)
            self.tableView.backgroundColor = UIColor(patternImage: UIImage(named: "pattern_clear_white.pdf")!)
            // remove uiview
            UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                if let viewWithTag = self.view.viewWithTag(101)
                {
                    viewWithTag.alpha = 0
                }
                }, completion: nil)
        }
    }
}

// refresh existing content
func tableViewRefresh(result: (done: Bool) -> Void){
    var query = PFQuery(className: "Posts")
    query = segueDataChecker()
    // limit objects
    query.limit = self.objects.count
    // save number of objects already loaded
    let actualNumberOfPosts = self.objects.count
    // get objects
    query.findObjectsInBackgroundWithBlock { (posts: [AnyObject]?, error: NSError?) -> Void in
        if error != nil
        {
            result(done: true)
            let alertView = createDefaultAlertError(message: "\(error!.localizedDescription.capitalizedString)", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil)])
            self.presentViewController(alertView, animated: true, completion: nil)
        }
        else
        {
            // clean array
            self.objects.removeAll()
            // populate array with same updated objects
            for eachPost in posts!
            {
                if self.objects.count < actualNumberOfPosts
                {
                    self.objects.append(eachPost as! PFObject)
                }
            }
            // reload and send back it has finished
            self.tableView.reloadData()
            result(done: true)
        }
    }
}

// load next results
func tableViewLoadMore(result: (done: Bool) -> Void){
    var query = PFQuery(className: "Posts")
    query = segueDataChecker()
    // skip objects already loaded
    query.skip = self.objects.count; // skip the first 20 results
    // limit results to next page amount
    query.limit = objectsPerPage
    // save number of objects already loaded
    let actualNumberOfPosts = self.objects.count
    // get objects
    query.findObjectsInBackgroundWithBlock { (posts: [AnyObject]?, error: NSError?) -> Void in
        if error != nil
        {
            result(done: true)
            let alertView = createDefaultAlertError(message: "\(error!.localizedDescription.capitalizedString)", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil)])
            self.presentViewController(alertView, animated: true, completion: nil)
        }
        else
        {
            // if there anything new
            if posts?.count != 0
            {
                // populate array with new objects
                for eachPost in posts!
                {
                    if (self.objects.count < self.objectsPerPage + actualNumberOfPosts)
                    {
                        self.objects.append(eachPost as! PFObject)
                    }
                }
                // reload and send back it has finished
                self.tableView.reloadData()
                result(done: true)
                // slide tablview one result down
                let newIndexPath = NSIndexPath(forRow: actualNumberOfPosts, inSection: 0)
                self.tableView.scrollToRowAtIndexPath(newIndexPath , atScrollPosition: UITableViewScrollPosition.None, animated: true)
            }
                // if there is nothing new
            else
            {
                result(done: true)
            }
        }
    }
}