在我的Xcode项目中,我将项目加载到Array
,然后将其显示在UITableViewController
上。虽然这些项加载UITableViewController
是空的并且不美观,所以我想在加载数据时在UITableViewController
中放置一个活动指示符。我在底部复制了我的代码。 我如何实现这个?(我听说称为完成处理程序的东西可能会做到这一点,但我不知道如何使用它)
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
}
答案 0 :(得分:2)
您似乎有一个用于加载表视图数据的异步函数。你只需要将activityIndicator.stopAnimating()
放在那里。
以编程方式或通过UIActivityIndicatorView
向表视图控制器添加Storyboard
。
如果您选择在代码中执行此操作,则执行此操作,包括添加约束以将其保留在视图控制器的中间:
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()
内调用上述代码之前,请在班级的某个地方声明let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
。
您只需在loadAnimals
开始执行后立即启动动画,并在调用tableView.reloadData()
之前停止它。将其命名为`activityIndicator,然后执行以下操作:
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())
}
}
}
答案 1 :(得分:0)
//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
}
}