2017-08-15 20 views
0

在我的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

[在uitable视图广场活动的指标(可能的重复https://stackoverflow.com/questions/29311093/place-activity-indicator-over-uitable-视图) – MwcsMac

回答

1

看来你有一个加载表视图数据的异步函数。你只需要把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()之前阻止它。将它命名为'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()) 
     } 
    } 
} 
+1

大这个完美的作品!谢谢 – fphelp

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