我整天都在苦苦寻找发生问题的原因。
我的UICollectionView
个单元格包含UIButton
和其他UIView
s。
我的UICollectionView
有8个部分。
我希望在点击特定单元格时修改UIButton
的图像。
我触发了一个名为“checked”的方法来修改图像。
但它不是仅修改特定按钮的图像,而是修改其他单元格中的其他按钮。当我按下其中一个按钮时,它们都会再次“未经检查”。所以,它们是相互关联的,但我真的不知道为什么!
这是我的代码:
- (ExCell*) collectionView:(UICollectionView *)collectView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//create collection view cell
ExCell *cell = (ExCell *)[collectView dequeueReusableCellWithReuseIdentifier:@"ExCell" forIndexPath:indexPath];
// the index is the row number in section + the number of items in all previous sections
int index = (int)indexPath.row;
for(int k=0; k < indexPath.section ; k++){
index += nbItemsInPart[k];
}
//configure cell :
NSMutableString *text = [[NSMutableString alloc]initWithString:@"Practical Exercise "];
[text appendString:[NSString stringWithFormat:@"%d",index+1]];
[text appendString:@"\n"];
[text appendString:[titles objectAtIndex:index]];
cell.label.text = text;
[cell.reminder addTarget:self action:@selector(doSomething:) forControlEvents: UIControlEventTouchUpInside];
[cell.done addTarget:self action:@selector(checked:) forControlEvents:UIControlEventTouchUpInside];
[cell.doIt addTarget:self action:@selector(doExercise:) forControlEvents: UIControlEventTouchUpInside];
[cell.doIt setTag:index];
[cell setTag:indexPath.section];
// return the cell
return cell;
}
这是按下按钮时调用的方法(它可以工作,但结果会影响其他按钮......):
-(void)checked:(id)sender{
if([sender imageForState:UIControlStateNormal] == [UIImage imageNamed:@"checked.png"]){
[sender setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}else{
[sender setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
}
}
这是我的自定义单元格的实现:
@implementation ExCell
@synthesize imageView;
@synthesize label;
@synthesize reminder;
@synthesize done;
@synthesize doIt;
- (id)initWithFrame:(CGRect)aRect
{
self = [super initWithFrame:aRect];
{
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
done = [UIButton buttonWithType:UIButtonTypeCustom];
done.frame = CGRectMake(0, 0, 85, 112);
[done setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[self addSubview:done];
float widthLabel = 0.0f;
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
widthLabel = (screenWidth-6)-158-156-3-85;
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
widthLabel = (screenHeight-24)-158-156-3-85;
}
//we create the UIImageView in this overwritten init so that we always have it at hand.
imageView = [[UIImageView alloc] init];
imageView.opaque = YES;
imageView.frame = CGRectMake(86, 0, widthLabel, 112);
[imageView setBackgroundColor:[UIColor whiteColor]];
imageView.alpha = 0.7;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//set specs and special wants for the imageView here.
[self addSubview:imageView]; //the only place we want to do this addSubview: is here!
label = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, widthLabel-5, 112)];
[label setBackgroundColor:[UIColor clearColor]];
label.userInteractionEnabled = NO;
label.numberOfLines = 0;
[self addSubview:label];
widthLabel = widthLabel+85+2;
doIt = [UIButton buttonWithType:UIButtonTypeCustom];
doIt.frame = CGRectMake(widthLabel, 0, 156, 112);
doIt.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[doIt setImage:[UIImage imageNamed:@"faireExo.png"] forState:UIControlStateNormal];
[self addSubview:doIt];
widthLabel = widthLabel+1+156;
reminder = [UIButton buttonWithType:UIButtonTypeCustom];
reminder.frame = CGRectMake(widthLabel, 0, 158, 112);
reminder.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[reminder setImage:[UIImage imageNamed:@"rappel.png"] forState:UIControlStateNormal];
[self addSubview:reminder];
}
return self;
}
@end
答案 0 :(得分:0)
由于细胞重复使用,您无法通过在按钮的操作方法中设置图像来实现。相反,您应该有一个可变数组来跟踪要检查图像的单元格的indexPaths。您可以在按钮的操作方法中从数组中添加(或删除)这些路径,但实际上是根据数组中的索引路径在cellForItemAtIndexPath中设置图像。此示例用于表视图,但集合视图的过程相同。
@property (strong,nonatomic) NSMutableArray *checkedPaths; // I instantiate this in viewDidLoad
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[cell.button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
if ([self.checkedPaths containsObject:indexPath]) {
[cell.button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
}else{
[cell.button setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}
return cell;
}
-(void)customActionPressed:(UIButton *) sender {
id superView = sender.superview;
while (superView && ![superView isKindOfClass:[UITableViewCell class]]) {
superView = [superView superview];
}// this while loop finds the cell that the button is a subview of
NSIndexPath *selectedPath = [self.tableView indexPathForCell:(UITableViewCell *)superView];
if ([self.checkedPaths containsObject:selectedPath]) {
[self.checkedPaths removeObject:selectedPath];
}else{
[self.checkedPaths addObject:selectedPath];
}
[self.tableView reloadRowsAtIndexPaths:@[selectedPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // you would use reloadItemsAtIndexPaths: for a collection view
}
答案 1 :(得分:0)
解决了这个问题!我不得不调用[collectionview reloadData]:
-(void)checked:(id)sender{
UIButton *button = (UIButton *)sender;
NSNumber *indice = [NSNumber numberWithInt:(int)button.tag];
if ([self.checkedPaths containsObject:indice]){
[self.checkedPaths removeObject:indice];
}else{
[self.checkedPaths addObject:indice];
}
[collectionView reloadData];
}
非常感谢!!