2017-03-01 16 views
0

所以我有這樣的功能:函數內的tableView:的cellForRowAtIndexPath:只調用進行第2次電池

func addActivityIndicator(_ aspectRatio: CGFloat?, index: Int) { 
     if activityIndicator == nil { 
      print("add activity indicator") 
      print("at index: \(index)") 
      let screenWidth = UIScreen.main.bounds.width 
      let height: CGFloat 
      if let aspectRatio = aspectRatio { 
       height = screenWidth/aspectRatio 
      } else { 
       print("aspect ratio is nil") 
       height = screenWidth 
      } 
      let indicatorFrame = CGRect(x: (screenWidth - 60)/2, 
             y: (height - 60)/2, 
             width: 60, 
             height: 60) 

      self.activityIndicator = NVActivityIndicatorView(frame: indicatorFrame, 
                  type: .ballClipRotateMultiple, 
                  color: .lightGray, 
                  padding: nil) 
      self.addSubview(self.activityIndicator!) 
      self.activityIndicator?.startAnimating() 
     } 
    } 

這基本上只是在上海華的中央添加活動的指標。 然後我內部cellForRow調用此函數:AtIndexPath:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let contentCell = tableView.dequeueReusableCell(withIdentifier: "ContentCell", for: indexPath) as! ContentCell 
    contentCell.thumbnail.addActivityIndicator(content.aspectRatio, index: indexPath.row) 

    return contentCell 
} 

縮略圖是將被添加一個活動的指標一個UIImageView。但在日誌中我只看到這一點:

附加活動的指標在指數:0

附加活動的指標,在​​指標:1個

附加活動的指標在指數:2

我如何使addActivityIndicator函數適用於所有單元?

+2

這是什麼問題? – vadian

+1

tbh,很難理解這個問題,但我想你是問爲什麼活動指標只添加到3個單元格中,但是你有,比如說20 可能的原因是你有3個單元格可見。 tableview不會立即實例化所有單元格,而是重新使用它們。 – RinaLiu

+0

但我滾動整個tableview後打印日誌。忘了提到這一點。 – samitarmum

回答

1

在func上添加ActivityIndi​​cator awakeFromNib的ContentCell

相關問題