在 UITableView 中加载数据时出现活动指示器问题 [英] Issue with Activity Indicator While loading data in UITableView

查看:26
本文介绍了在 UITableView 中加载数据时出现活动指示器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php 从服务器获取记录

I'm getting record from server using php

由于我目前有 11 条记录,所以我想在开始时只显示 6 条记录,剩余的下 5 条记录将在用户滚动时到达最后一个单元格时显示.所以这个过程是在工作形式,但问题是在运行时,它工作得如此之快,以至于在到达最后一行之前,滚动时所有记录都已经显示出来,而活动指示器只是在 tableView 的底部动画.

As i'm getting 11 record currently so i want in starting i will show just 6 records and remaining next 5 record will show when user reached at last cell while scrolling. So this process is in working form, but the problem is while running, it working so fast that before reaching last row all records are already showing while scrolling and the activity indicator is just animating at the bottom of tableView.

我不知道是什么问题.

我还希望当用户到达最后一个单元格时,活动指示器在加载数据时开始动画.

Also i want when user reached at last cell, activity indicator start animating while loading data .

这是我的代码

import UIKit
import AlamofireImage

struct property{
let property_Id     : String
let propertyTitle   : String
let imageURL        : String
let user_Id         : String
let area            : String
let bed             : String
let unit            : String
let bath            : String
let price           : String
}
class searchRecordsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,favouriteButtonTableViewCellDelegate {

var y                           = 6
var m                           = 12
@IBOutlet weak var tableView    : UITableView!
var RESULT                      : [NSDictionary] = []
var myProperty                  = [property]()
var myPropertyCopy              = [property]()

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

//MARK: Getting dictionary data from previous controller

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationItem.title = "Results"
    self.navigationController?.navigationBar.tintColor = UIColor.black

    //MARK: Getting dictionary data from previous controller
    for item in RESULT  {

        let propertyInfo = property(property_Id: String(item["propertyId"]! as! Int), propertyTitle: item["propertyTitle"]! as! String, imageURL: item["imagePath"]! as! String, user_Id: String(item["userId"]! as! Int), area: item["area"]! as! String, bed: item["bed"]! as! String, unit: item["unit"]! as! String, bath: item["bath"]! as! String, price: item["price"]! as! String )

        myProperty.append(propertyInfo)

    }
    //MARK: Inserting first 6 records in Array
    for i in 0 ..< 6 {
        if !(myProperty.indices.contains(i)) {
            break
        }
        myPropertyCopy.append(myProperty[i])

    }

}


func downloadImage(imagePath : String, theIMAGEVIEW : UIImageView) {
    let myUrl = URL(string: URL_IP+imagePath);

    //MARK: AlamofireImage to download the image
    theIMAGEVIEW.af_setImage(withURL: myUrl!, placeholderImage: #imageLiteral(resourceName: "addProperty"), filter: nil, progress: nil, runImageTransitionIfCached: true, completion: nil)
}

func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
    return myPropertyCopy.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecordCell") as! searchrecordsTableViewCell

    let property = myPropertyCopy[indexPath.row]
    cell.area.text = property.area+" "+property.unit
    if property.bath == ""{
        cell.bath.text = property.bath
    }
    else{
        cell.bath.text = property.bath+" Baths"
    }
    if property.bed == ""{
        cell.bed.text = property.bed
    }
    else{
        cell.bed.text = property.bed+" Baths"
    }
    cell.propertyTitle.text = property.propertyTitle
    cell.price.text = convertAMOUNT(price : property.price)
    downloadImage(imagePath: property.imageURL, theIMAGEVIEW: cell.myImageView)
    //----
    cell.delegate = self

    return cell
}


func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if myPropertyCopy.count != myProperty.count{
        let lastRow = myPropertyCopy.count - 1
        if indexPath.row == lastRow {
            let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
            spinner.startAnimating()
            spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))

            self.tableView.tableFooterView = spinner
            self.tableView.tableFooterView?.isHidden = false
            moreData()

        }

    }
}

func moreData(){

    for i in y ..< m {
        if !(myProperty.indices.contains(i)) {

            break

        }
        myPropertyCopy.append(myProperty[i])

    }
        y = y + 10
        m = m + 10
        self.tableView.reloadData()

}



}

目前我的 TableView 看起来像在此处查看输出

currently my TableView looks like See output here

提前致谢.

推荐答案

经过长时间的练习,我已经解决了我的问题,我刚刚添加了 UIScrollView 委托函数,用于检查用户是否到达最后一个单元格,然后启动 activityIndi​​cator 3 秒,然后然后加载数据,就是这样.

After a long practice i have done my problem, i have just added UIScrollView delegate function for checking if the user reached at last cell then start activityIndicator for 3 seconds and then load data and that's it.

由于我只有非常少量的数据,这就是为什么我在获取数据之前开始使用 UIactivityIndi​​cator 3 秒钟.

As i have just very small amount of data that's why i'm start ing UIactivityIndicator for 3 seconds before getting data.

 func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    // UITableView only moves in one direction, y axis
    let currentOffset = scrollView.contentOffset.y
    let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height

    if maximumOffset - currentOffset <= 10.0 {
        let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)

        if myPropertyCopy.count != myProperty.count {

            //print("this is the last cell")

            spinner.startAnimating()
            spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
            spinner.hidesWhenStopped = true
            self.tableView.tableFooterView = spinner
            self.tableView.tableFooterView?.isHidden = false

            DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                //MARK: Loading more data
                self.moreData()

            }

        }
        else{
            self.tableView.tableFooterView?.isHidden = true
            spinner.stopAnimating()
        }
    }

}

这篇关于在 UITableView 中加载数据时出现活动指示器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆