将活动指示器设置为CollectionViewCell

时间:2016-04-23 06:22:18

标签: ios swift uicollectionview uicollectionviewcell uiactivityindicatorview

我正在尝试为每个集合视图单元格显示初始加载时间的活动指示器。我尝试将其放在cellForItemAtIndexPath中,但由于某种原因,它会在单元格上方创建20像素。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("AACollectionViewCell", forIndexPath: indexPath) as! AACollectionViewCell

    let activityView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)

    activityView.center = cell.theImage.center
    // Also tried
    activityView.center = cell.contentView.center

    activityView.startAnimating()

    return cell
}

处理它的正确方法是什么?

编辑:我也试过了,但它没有在任何地方创建activityIndi​​cator

    let activityView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
    activityView.center = cell.contentView.center
    cell.contentView.addSubview(activityView)

1 个答案:

答案 0 :(得分:4)

cell.contentView.center从超视图的坐标系中为您提供contentView的帧。你应该做什么:

activityView.center = CGPointMake(cell.contentView.frame.size.width / 2, cell.contentView.frame.size.height / 2)