将最后一个自定义 collectionViewcell 添加到 collectionView swift 3

add last custom collectionViewcell to collectionView swift 3

我正在尝试使用 2 个不同的自定义 collectionviewcell 构建 collectionView。我想要的是

请忽略第一个按钮(邀请发送一个)我们决定将其删除。我只想每次都在末尾有加号按钮(添加用户一)

我继续使用我在 google 上发现的许多不同方法时遇到错误。 我有的是

import UIKit
import SDWebImage

private let reuseIdentifier = "safeCircleCell"
private let lastCell = "LastSafeCircleCell"

class SafeCircleCollectionViewController: UICollectionViewController {

var circleArray: [CircleUser] = [];
var selectedEntry: CircleUser!

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // TODO Need to add logic to add + button
    PackageGuardService.shared.getCircleUsers() {
        (circleUsers, error)in
        self.circleArray = circleUsers!
        self.collectionView?.reloadData()
    }
   collectionView?.register(LastSafeCircleCell.self, forCellWithReuseIdentifier: lastCell)
}

和.........

   override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return self.circleArray.count + 1
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.row == circleArray.count {

        //try to set the add User button at the last cell

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: lastCell, for: indexPath)as? LastSafeCircleCell;
    //            cell?.btnLabel.text = "add friend"
        print("last Cell")
        return cell!
    } else {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)as? SafeCircleCell;

      // set all the user pic and info here and this part work great

            cell?.safeCircleImage.layer.borderWidth = 2
    cell?.safeCircleImage.contentMode = .scaleAspectFill
    cell?.safeCircleImage.layer.cornerRadius = (cell?.safeCircleImage.bounds.width)!/2
    cell?.safeCircleImage.layer.masksToBounds = true
    cell?.safeCircleImage.layer.borderWidth = 0.5

    let circleEntry = self.circleArray[indexPath.row]
    print("safe circle array image \(circleEntry.ImageUrl)")


    cell?.setUser(circleUser: circleEntry)

    return cell!
    }
}

在我的 LastSafeCircleCell.swift 文件中

 import UIKit

 class LastSafeCircleCell: UICollectionViewCell {
 let addCircleButton: UIButton = {
    let button = UIButton()
    button.backgroundColor = UIColor.white
    button.layer.cornerRadius = 18
    button.clipsToBounds = true
    button.setImage(UIImage(named: "add-safe-circle"), for: .normal)

    return button
}()


let btnLabel: UILabel = {
    let label = UILabel()
    label.text = "add Friend"
    return label
}()
func addViews(){
    backgroundColor = UIColor.black

    addSubview(addCircleButton)
    addSubview(btnLabel)
}
    override init(frame: CGRect) {
        super.init(frame: frame)
        print("lastCell in")
        addViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  }

我认为问题是由于您在注册 class LastSafeCircleCell 之前重新加载了表格视图,所以我认为您应该注册 class LastSafeCircleCellviewDidLoad 方法而不是 viewWillAppear 方法中,还因为您不需要在每次出现视图时注册 class

现在要让单元格显示而不是黑色,您需要设置 LastSafeCircleCell 的框架,直接设置框架或使用自动布局约束,然后设置其属性,如背景颜色。

我想给的另一个建议是:

而不是

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: lastCell, for: indexPath)as? LastSafeCircleCell;

使用这个:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: lastCell, for: indexPath) as! LastSafeCircleCell

我在上面的代码中做了两处更改,如果你在一行上写一个语句,你不需要在 swift 中使用 ;。我所做的第二个更改是在此处使用 as! 强制解包可选,这是一种情况,如果您不正确地对单元格进行双列队列,您希望程序崩溃。

所以现在在您的以下代码中,您可以简单地使用 cell 而不是 cell! 并对 else 部分中的其他单元格执行相同的操作。