问题与活动指示灯虽然UITableView中加载数据(Issue with Activity Ind

2019-10-29 07:53发布

我是从服务器使用PHP获取记录

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

我不知道是什么问题。

此外,我想,当用户在最后一个单元达到,活动的指标开始在加载数据的动画。

这里是我的代码

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看起来像见这里输出

提前致谢。

Answer 1:

经过长期的实践我已经完成了我的问题,我刚才说的UIScrollView委托功能检查,如果在最后一个单元达到用户然后开始activityIndi​​cator 3秒,然后加载数据,仅此而已。

正如我刚才非常小的数据量,这就是为什么我开始获取数据之前,荷兰国际集团UIactivityIndi​​cator 3秒。

 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()
        }
    }

}


文章来源: Issue with Activity Indicator While loading data in UITableView