Activity Tableview 加载时的指示器

Activity Indicator While Tableview Loads

在我的 Xcode 项目中,我将项目加载到 Array 中,然后将其呈现在 UITableViewController 中。虽然这些项目加载 UITableViewController 是空的并且不美观所以我想在加载数据时在 UITableViewController 中放置一个 activity 指示符。我已经在底部复制了我的代码。 我如何实现它?(我听说一种叫做完成处理程序的东西可能会起作用,但我不确定如何使用它)

func loadAnimals() {
    let animalQuery = PFQuery(className: "Animals")
    animalQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which user
    animalQuery.limit = 10
    animalQuery.findObjectsInBackground { (objects, error) in
        if error == nil {
            self.colorArray.removeAll(keepingCapacity: false)
            self.animalNameArray.removeAll(keepingCapacity: false)                
            self.animalTypeArray.removeAll(keepingCapacity: false)

            for object in objects! {
                self.colorArray.append(object.value(forKey: "colorType") as! String) // add data to arrays
                self.animalNameArray.append(object.value(forKey: "animalName") as! String) // add data to arrays                    
                self.animalTypeArray.append(object.value(forKey: "animalType") as! String) // add data to arrays
            }
            self.tableView.reloadData()

        } else {
            print(error?.localizedDescription ?? String())
        }
    }
}

//按行放置颜色

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! AnimalCell //connects to color cell

    //Adds the animal information in the cells
    cell.colorType.text = colorArray[indexPath.row] 
    cell.animalName.text = animalNameArray[indexPath.row] 
    cell.animalType.text = animalTypeArray[indexPath.row] 

    return cell
}
//SOMEWHERE ELSE IN YOUR CODE    
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge) 
    activityIndicator.hidesWhenStopped = true
    self.view.addSubview(activityIndicator)

///.... where you load animals
func loadAnimals {
///
    activityIndicator.startAnimating()
     ////your code
     ///...
     animalQuery.findObjectsInBackground { (objects, error) in
       if error == nil {
        ///Stop the spinner
        activityIndicator.stopAnimating()
        ///...rest of your code
        }
       }

您似乎有一个用于加载 table 视图数据的异步函数。你只需要把 activityIndicator.stopAnimating() 放在那里。

以编程方式或通过 Storyboard 添加 UIActivityIndicatorView 到您的 table 视图控制器。 如果您选择在代码中执行此操作,这就是您的操作方式,包括添加约束以将其保持在视图控制器的中间:

view.addSubview(activityIndicator)
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
activityIndicator.hidesWhenStopped = true
activityIndicator.color = UIColor.black
let horizontalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)
view.addConstraint(verticalConstraint)

viewDidLoad() 中调用上面的代码之前,在 class.

的某处声明 let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)

您只需要在 loadAnimals 开始执行后立即开始动画,并在调用 tableView.reloadData() 之前停止它。将其命名为 `activityIndi​​cator,然后执行以下操作:

func loadAnimals() {
    activityIndicator.startAnimating()
    let animalQuery = PFQuery(className: "Animals")
    animalQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which user
    animalQuery.limit = 10
    animalQuery.findObjectsInBackground { (objects, error) in
        if error == nil {
            self.colorArray.removeAll(keepingCapacity: false)
            self.animalNameArray.removeAll(keepingCapacity: false)                
            self.animalTypeArray.removeAll(keepingCapacity: false)

            for object in objects! {
                self.colorArray.append(object.value(forKey: "colorType") as! String) // add data to arrays
                self.animalNameArray.append(object.value(forKey: "animalName") as! String) // add data to arrays                    
                self.animalTypeArray.append(object.value(forKey: "animalType") as! String) // add data to arrays
            }
            activityIndicator.stopAnimating()
            self.tableView.reloadData()

        } else {
            print(error?.localizedDescription ?? String())
        }
    }
}