我想在Collection视图的标题中添加几个按钮。从故事板启用“Section Header”。然后将“视图”拖动到该标题。并将其背景颜色设为绿色
答案 0 :(得分:3)
使用此方法
在CollectionHeaderCell
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath as IndexPath)
headerView.backgroundColor = UIColor.blue;
return headerView
case UICollectionElementKindSectionFooter:
let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath as IndexPath)
footerView.backgroundColor = UIColor.green;
return footerView
default:
assert(false, "Unexpected element kind")
}
}
//为每个单元格索引路径创建一个单元格
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InviteFriendCell", forIndexPath: indexPath)
let profileimage: UIImageView = (cell.viewWithTag(5) as! UIImageView)
profileimage.image = UIImage(named:arrayofdata[indexPath.row]["image"]!)
return cell
}