当我切换dataSource时如何让所选单元格仍被选中?
gif图像显示当前情况。
我不想那样,当我点击开关按钮时,如果我选择“混合”,我想要仍然选择“单”和“双”,并回到“组合玩法”,它们也被选中。
怎么办?
注意,当我单击切换按钮时,dataSource也会切换,但我可以使用相同的dataSource(有一种类型可以识别4种类型,它们都包含在原始dataSource中)。
代码如下:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (collectionView == self.buy_code_cv) {
Lucky28BuyCodeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Lucky28BuyCodeCell" forIndexPath:indexPath];
cell.model = self.dataSource_code[indexPath.row];
cell.delegate = self;
return cell;
} else {
Lucky28PlayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Lucky28PlayCell" forIndexPath:indexPath];
if (self.selected_button == 0) {
cell.model = self.dataSource_method[indexPath.row];
}else if (self.selected_button == 1) {
cell.model = self.dataSource_mix[indexPath.row];
}else if (self.selected_button == 2) {
cell.model = self.dataSource_color[indexPath.row];
}else {
cell.model = self.dataSource_leopard[indexPath.row];
}
cell.delegate = self;
return cell;
}
}
单元格中的setSelected方法:
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
//self.selected = !selected;
if (selected) {
self.backView.backgroundColor = APP_COLOR;
self.number_label.textColor = [UIColor whiteColor];
self.multiple_label.textColor = [UIColor whiteColor];
}
else {
self.backView.backgroundColor = [UIColor whiteColor];
self.number_label.textColor = HexRGB(0x999999);
self.multiple_label.textColor = HexRGB(0xcccccc);
}
if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedBuyCodeCell:)]) {
[self.delegate didSelectedBuyCodeCell:self];
}
}
怎么办?
答案 0 :(得分:0)
创建一个selectFlag数组来存储状态,并在cellForItemAtIndexPath
答案 1 :(得分:0)
您应该管理所选单元格的数组。我的意思是你必须跟踪你选择的细胞。因此,在cellForItemAtIndexPath
检查是否选中了单元格(从数组或数据源或跟踪选定状态的位置),然后显示所选状态的背景颜色和文本颜色。
你可以选择单元格,
[cell setSelected:YES];
您应该从setSelected
方法修改(所选单元格的)跟踪记录。
答案 2 :(得分:0)
在切换数据源之前保存索引路径。
NSArray *indexPaths = collectionView.indexPathsForSelectedItems;
切换数据源后。
for (NSIndexPath *indexPath in indexPaths) {
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
// if you are doing anything with UI on didSelect, you have to call it manually.
//[self collectionView:collectionView didSelectItemAtIndexPath:indexPath]
}
答案 3 :(得分:0)
您已经有4个阵列来保存分组模型,程序结构足以让您轻松解决问题。 (主要思想,未在IDE中测试。提供了实现思路,代码未在IDE中测试,末尾有中文总结)
首先,在模型中添加新属性以进行录制选择:
<uses-permission android:name="android.permission.INTERNET"/>
第二次,将选择保存在集合视图委托方法中:
@interface YourModel : NSObject
//...Other properties
@property (nonatomic, assign) BOOl selected;
}
第三次,在刷新集合视图时恢复单元格的选择:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
YourModel *model;
if (self.selected_button == 0) {
model = self.dataSource_method[indexPath.row];
}else if (self.selected_button == 1) {
model = self.dataSource_mix[indexPath.row];
}else if (self.selected_button == 2) {
model = self.dataSource_color[indexPath.row];
}else {
model = self.dataSource_leopard[indexPath.row];
}
model.selected = YES; //key point!
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
YourModel *model;
if (self.selected_button == 0) {
model = self.dataSource_method[indexPath.row];
}else if (self.selected_button == 1) {
model = self.dataSource_mix[indexPath.row];
}else if (self.selected_button == 2) {
model = self.dataSource_color[indexPath.row];
}else {
model = self.dataSource_leopard[indexPath.row];
}
model.selected = NO; //key point!
}
中文总结:
model里提供属性存储是否选中;
collection view的选中和非选中事件中将值保留到model中;
重刷收集视图后,将模型中的是否选中值反应到单元的界面中。