【发布时间】:2015-05-07 12:15:58
【问题描述】:
我在每个单元格中有UICollectionview 控制器和UIButton。在选择按钮时,我必须更改按钮 bg 图像,双击时我必须再次更改它。 (单击喜欢,双击喜欢用户兴趣页面上的概念)。
做这个的最好方式是什么?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [interestsCollection dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIButton* img=[[UIButton alloc]init];
[img setImage:[UIImage imageNamed:[imgTitleArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
[img addTarget: self action: @selector(interestClicked:) forControlEvents: UIControlEventTouchUpInside] ;
img.frame=CGRectMake(10, 10, 60, 60);
[cell.contentView addSubview:img];
UILabel* lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 75, 80, 20)];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.text=[titleArray objectAtIndex:indexPath.row];
lbl.font = [UIFont fontWithName:@"Arial" size:11];
lbl.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0];
[cell.contentView addSubview:lbl];
return cell;
}
【问题讨论】:
-
这个方法你实现了吗? -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {}
-
是的,我有那个方法,但是如何在选择时更改选定单元格的子视图(按钮)背景?
-
-(void)interestClicked:(id)sender{ UIButton *button = (UIButton *)sender; //在此处更改按钮图像 [collectionView reloadData]; }
标签: ios objective-c uicollectionview uicollectionviewcell