samitarmum
samitarmum

Reputation: 83

function inside tableView:cellForRowAtIndexPath: only called for the first 2 cell

So I have this function:

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

which just basically add an activity indicator in the center of the superview. and then I called this function inside 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
}

thumbnail is a uiimageview which will be added an activity indicator. But in the log I only see this:

add activity indicator at index: 0

add activity indicator at index: 1

add activity indicator at index: 2

How do I make the addActivityIndicator function works on all cell?

Upvotes: 0

Views: 153

Answers (1)

DiegoQ
DiegoQ

Reputation: 1116

Add the ActivityIndicator on func awakeFromNib of ContentCell

Upvotes: 1

Related Questions