如果我在 UICollectionView 外单击 UIButton,是否可以处理 UICollectionViewCell
is it possible to handle UICollectionViewCell if i click UIButton outside UICollectionView
首先,对不起,我的英语不好。
无论如何,当我在 UICollectionView 外单击 UIButton 时,我想 select UICollectionViewCell 中的所有单元格。但是我不会怎么办
我认为使用像 5 星这样的 UIButton 集合是可能的。我试了一下。
嗯..点击下面的按钮。蓝色的单元格是错误的。
Main view
如果单击 'Everyday' 按钮,所有单元格都将变为红色。红色单元格表示它已被选中。
Selected Everyday
如果你点击'Weekend'按钮,最右边的两个单元格会变成红色。现在你知道这个 UICollectionView 意味着 'a week'。因此,当 'Weekend' 被 select 编辑时,其他 5 天将是蓝色的。
Selected Weekend
我用 UICollectionView 做了这个。但我不知道如何用外面的按钮控制。如果您需要更多信息和代码,或者您不明白我会编辑它或发表评论。请告诉我怎么做。
ViewController 关于 UIButton 和 UICollectionView 的代码在这里。在 CollectionViewCell 中有一个图像视图...仅此而已...
import UIKit
class CreateViewController: UIViewController {
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var MotivTextField: UITextField!
@IBOutlet weak var repeatCollectionView: UICollectionView!
@IBAction func everydayBtn(_ sender: Any) {
// empty
}
@IBAction func weekdayBtn(_ sender: Any) {
// empty
}
@IBAction func weekendBtn(_ sender: Any) {
// empty
}
var selectedDay = [Bool]()
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...7 {
self.selectedDay.append(false)
}
repeatCollectionView.delegate = self
repeatCollectionView.dataSource = self
repeatCollectionView.isScrollEnabled = false
}
}
extension CreateViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? RepeatDayCVCell {
cell.dayBtn.backgroundColor = UIColor.red
}
}
}
extension CreateViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RepeatDayCVCell", for: indexPath) as! RepeatDayCVCell
if self.selectedDay[indexPath.row] == true {
cell.dayBtn.backgroundColor = UIColor.red
}
else {
cell.dayBtn.backgroundColor = UIColor.blue
}
return cell
}
}
您可以操纵您的模型,我的意思是关于数组 selectedDay。这取决于具体情况 - 您可以在特定索引处设置 true/false。更新数组后,您必须使用 reloadData() 方法重新加载数据才能在 UI 层查看效果。
@IBAction func everydayBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = true
}
repeatCollectionView.reloadData()
}
@IBAction func weekdayBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = i < 5 ? true : false
}
repeatCollectionView.reloadData()
}
@IBAction func weekendBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = i > 5 ? true : false
}
repeatCollectionView.reloadData()
}
方法 collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 现在不需要了。如果您点击任何单元格,它会将颜色更改为红色。
您必须更新数据模型并在每个按钮操作上重新加载集合视图。
@IBAction func weekendBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
if index < 5 {
selectedDay[index] = false
} else {
selectedDay[index] = true
}
}
repeatCollectionView.reloadData()
}
@IBAction func weekdayBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
if index < 5 {
selectedDay[index] = true
} else {
selectedDay[index] = false
}
}
repeatCollectionView.reloadData()
}
@IBAction func everydayBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
selectedDay[index] = true
}
repeatCollectionView.reloadData()
}
如果您想在点击单元格时更新集合视图,请更新 selectedDay
中该索引处的数据并重新加载该行。
请原谅格式化。
首先,对不起,我的英语不好。
无论如何,当我在 UICollectionView 外单击 UIButton 时,我想 select UICollectionViewCell 中的所有单元格。但是我不会怎么办
我认为使用像 5 星这样的 UIButton 集合是可能的。我试了一下。
嗯..点击下面的按钮。蓝色的单元格是错误的。
Main view
如果单击 'Everyday' 按钮,所有单元格都将变为红色。红色单元格表示它已被选中。
Selected Everyday
如果你点击'Weekend'按钮,最右边的两个单元格会变成红色。现在你知道这个 UICollectionView 意味着 'a week'。因此,当 'Weekend' 被 select 编辑时,其他 5 天将是蓝色的。
Selected Weekend
我用 UICollectionView 做了这个。但我不知道如何用外面的按钮控制。如果您需要更多信息和代码,或者您不明白我会编辑它或发表评论。请告诉我怎么做。
ViewController 关于 UIButton 和 UICollectionView 的代码在这里。在 CollectionViewCell 中有一个图像视图...仅此而已...
import UIKit
class CreateViewController: UIViewController {
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var MotivTextField: UITextField!
@IBOutlet weak var repeatCollectionView: UICollectionView!
@IBAction func everydayBtn(_ sender: Any) {
// empty
}
@IBAction func weekdayBtn(_ sender: Any) {
// empty
}
@IBAction func weekendBtn(_ sender: Any) {
// empty
}
var selectedDay = [Bool]()
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...7 {
self.selectedDay.append(false)
}
repeatCollectionView.delegate = self
repeatCollectionView.dataSource = self
repeatCollectionView.isScrollEnabled = false
}
}
extension CreateViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? RepeatDayCVCell {
cell.dayBtn.backgroundColor = UIColor.red
}
}
}
extension CreateViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RepeatDayCVCell", for: indexPath) as! RepeatDayCVCell
if self.selectedDay[indexPath.row] == true {
cell.dayBtn.backgroundColor = UIColor.red
}
else {
cell.dayBtn.backgroundColor = UIColor.blue
}
return cell
}
}
您可以操纵您的模型,我的意思是关于数组 selectedDay。这取决于具体情况 - 您可以在特定索引处设置 true/false。更新数组后,您必须使用 reloadData() 方法重新加载数据才能在 UI 层查看效果。
@IBAction func everydayBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = true
}
repeatCollectionView.reloadData()
}
@IBAction func weekdayBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = i < 5 ? true : false
}
repeatCollectionView.reloadData()
}
@IBAction func weekendBtn(_ sender: Any) {
for i in 0...7 {
self.selectedDay[i] = i > 5 ? true : false
}
repeatCollectionView.reloadData()
}
方法 collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 现在不需要了。如果您点击任何单元格,它会将颜色更改为红色。
您必须更新数据模型并在每个按钮操作上重新加载集合视图。
@IBAction func weekendBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
if index < 5 {
selectedDay[index] = false
} else {
selectedDay[index] = true
}
}
repeatCollectionView.reloadData()
}
@IBAction func weekdayBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
if index < 5 {
selectedDay[index] = true
} else {
selectedDay[index] = false
}
}
repeatCollectionView.reloadData()
}
@IBAction func everydayBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
selectedDay[index] = true
}
repeatCollectionView.reloadData()
}
如果您想在点击单元格时更新集合视图,请更新 selectedDay
中该索引处的数据并重新加载该行。
请原谅格式化。