gpt4 book ai didi

ios - Collection View reloadData 方法显示选中的单元格

转载 作者:行者123 更新时间:2023-11-29 10:26:21 24 4
gpt4 key购买 nike

我目前有一个 collectionView,其中包含用户可以选择的单元格(可以是多个),点击记录按钮后,这些选定单元格的值将被记录下来。除了记录之外,我还想清除 collectionView 中的选择,以便重复该过程。我目前在记录按钮中有 collectionView.reloadData() 但它不会清除用户的选择,而是在不执行选择的情况下将单元格显示为已选择(选择将确定屏幕上的操作)。这个选定的单元格总是在变化;它跳来跳去,但总是以相同的顺序。有人知道为什么会这样吗?另外,reloadData() 是清除 collectionView 选择的正确/有效方法吗?

这是我注册 CustomCell Nib 的地方

viewDidLoad()
{

let nib3 = UINib(nibName: "CustomCell", bundle: nil)
collectionView.registerNib(nib3, forCellWithReuseIdentifier: "cell")
collectionView.delegate = self
collectionView.dataSource = self
}

下面是我实现的Delegate方法和DataSource方法

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return outcomeList.count
}

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

let title:String = outcomeList[indexPath.row]
let cell:CustomCell = self. collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCell
cell.label.text = title
cell.tag = indexPath.row + 19
cell.selected = false
return cell

}


func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.collectionView.deselectItemAtIndexPath(indexPath, animated: true)
let cell: CustomCell = collectionView.cellForItemAtIndexPath(indexPath) as! CustomCell
cell.layer.borderColor = UIColor.whiteColor().CGColor
cell.backgroundColor = UIColor.redColor()
cell.label.textColor = UIColor.whiteColor()


cellSelected(cell)

}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell:CustomCell = collectionView.cellForItemAtIndexPath(indexPath) as! CustomCell
cell.layer.borderColor = UIColor.redColor().CGColor
cell.backgroundColor = UIColor.whiteColor()
cell.label.textColor = UIColor.redColor()
}

这是我在选择单元格时调用的方法

func cellSelected(sender: CustomCell)
{
let text:String = fieldedResultLabel.text!
resultLabel.text = text + sender.label.text! + "-"
}

这里是IBAction的记录

@IBAction func recordButtonTapped(sender: AnyObject) {
// cdao is a custom object that handles all my CoreData requests
cdao.storeResult(resultlabel)
// reset the collection View so that all cells show as not selected i do this by reloadData() but still shows some cells as selected
collectionView.reloadData()
}

最佳答案

UICollectionView,与 UITableView 非常相似,允许重复使用单元格。当您重新加载 View 时,之前选择的单元格将被重复使用,并且很可能不会用于同一位置(因此您会看到“跳来跳去”)。当您收到对 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPathUICollectionViewDelegate 调用时,请确保在您的实现主体中取消选择单元格。

例如

objective-C :

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
// do the rest of your processing of the selected item
}

swift :

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
collectionView.deselectItemAtIndexPath(indexPath, animated: true)
// do the rest of your processing of the selected item
}

这是可取的,因为它向用户提供了动画反馈,而不是显示自己被选中,然后突然通过 reloadData 重新绘制。在这一点上,您可能可以避免在每次选择后重新加载表格,因为您现在正在正确处理取消选择,而不是通过重新加载表格来蛮力。

更新

我创建了一个小示例应用程序,它可能会根据您的描述模仿您正在做的事情。代码很小,并且按照我描述的方式工作,无需在处理选定的单元格后重新加载 Collection View 。显然,我已经在 Storyboard 中设置了 View 和控件,但其要点应该是显而易见的:

class CustomCell : UICollectionViewCell
{
override var selected : Bool {
didSet {
self.layer.backgroundColor = (self.selected) ? UIColor.redColor().CGColor : UIColor.blueColor().CGColor
}
}
}

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet var collectionView : UICollectionView?;

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let collectionView = self.collectionView {
collectionView.registerClass(CustomCell.self, forCellWithReuseIdentifier: "Custom View")
collectionView.allowsMultipleSelection = true
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 16;
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Custom View", forIndexPath: indexPath) as! CustomCell
cell.selected = false
return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// collectionView.deselectItemAtIndexPath(indexPath, animated: true) can be called here...
// but if you are wanting the cells to retain selection visually,
// then wait until you batch process them below
}

@IBAction func handleButtonPress(sender : AnyObject ) {
// do your updates to your backing store, etc.

// manually deselect the cells for a pleasant user visual experience, or alternatively...
// invoke self.collectionView.reloadData() to bulk reload instead
if let selectedCells = self.collectionView?.indexPathsForSelectedItems() as? [NSIndexPath] {
for indexPath in selectedCells {
self.collectionView?.deselectItemAtIndexPath(indexPath, animated: true)
}
}
}
}

更新 2

感谢您的完整实现。问题在于,当从委托(delegate)回调 didSelectCellAtIndexPath 中选择单元格时,您会显式更改单元格的背景和文本颜色。由于您的代表的 didDeslectCellAtIndexPath 永远不会被调用(调用 collectionView.deselectItemAtIndexPath(indexPath, animated: true) 不会调用它)除非手动点击,否则您的单元格的背景和文本颜色不会重置,并在重用循环中保持“选定”颜色。返回并再次仔细查看我的示例,它几乎展示了针对您的问题的可能可行的解决方案。处理自定义单元格中的选定状态更改将确保它在选定状态更改时正确更新。

reloadData 适用于批量重置您的 Collection View ,除非获取支持数据的成本很高,在这种情况下,您最好获取所选单元格的列表并明确取消选择它们(这是我的代码也演示了)。显式取消选择将以动画方式取消选择,这在视觉上比批量重新加载更具吸引力。

关于ios - Collection View reloadData 方法显示选中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32015774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com