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

时间:2017-10-18 00:03:35

标签: swift swift3 uicollectionview uicollectionviewcell

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

enter image description here

请忽略第一个按钮(邀请发送一个)我们决定删除它。我只希望每次都有加号按钮(添加用户1)

我在谷歌上找到的许多不同的方法继续出错。 我拥有的是

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

1 个答案:

答案 0 :(得分:0)

我认为问题是由于您在注册课程LastSafeCircleCell之前正在重新加载tableview,所以我认为您应该在LastSafeCircleCell方法中注册课程viewDidLoad而不是{ {1}}方法,也因为每次出现视图时都不需要注册类

现在要显示单元格而不是黑色,您需要设置viewWillAppear的框架,直接设置框架或使用Autolayout约束,然后将其属性设置为背景颜色。

我想提出的另一个建议是:

而不是

LastSafeCircleCell

使用它:

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

我在上面的代码中做了两处更改,如果你在一行上写一个语句,你不需要在swift中使用let cell = collectionView.dequeueReusableCell(withReuseIdentifier: lastCell, for: indexPath) as! LastSafeCircleCell 。我做的第二个更改是使用;强制解压缩可选项,这是您希望程序在没有正确释放单元格时崩溃的情况之一。

所以现在在以下代码中,您只需使用as!代替cell,并对cell!部分中的其他单元格执行相同操作。