【发布时间】:2015-11-24 07:26:20
【问题描述】:
我想将按钮添加到单元格的左上角。我已经尝试过,但我总是在集合视图单元格中添加,而不是在单元格上,所以请告诉我怎么做?
我试过了。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BlogAlbumCell *cell;
static NSString *identifier = @"UserBlogAlbum";
cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UserAlbum *user_allbum=[arr_userAlbums objectAtIndex:indexPath.row];
cell.label_blog_name.text=user_allbum.album_name;
[cell.image_blog_image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_allbum.album_image]] placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]];
if([[[NSUserDefaults standardUserDefaults]valueForKey:@"longPressed"] isEqualToString:@"yes"])
{
NSLog(@"Inside the long press section");
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[anim setToValue:[NSNumber numberWithFloat:0.0f]];
[anim setFromValue:[NSNumber numberWithDouble:M_PI/50]];
[anim setDuration:0.1];
[anim setRepeatCount:NSUIntegerMax];
[anim setAutoreverses:YES];
cell.layer.shouldRasterize = YES;
[cell.layer addAnimation:anim forKey:@"SpringboardShake"];
CGFloat delButtonSize = 30;
UIButton *delButton = [[UIButton alloc] initWithFrame:CGRectMake(-5,-5, delButtonSize, delButtonSize)];
delButton.backgroundColor = [UIColor clearColor];
[delButton setImage: [UIImage imageNamed:@"cross_30.png"] forState:UIControlStateNormal];
[delButton setBackgroundColor:[UIColor blueColor]];
[cell addSubview:delButton];
delButton.tag=indexPath.row;
[delButton addTarget:self action:@selector(deleteAlbum:) forControlEvents:UIControlEventTouchUpInside];
}
else if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"singleTap"] isEqualToString:@"yes"])
{
if(indexPath.row==([arr_userAlbums count]-1))
{
[[NSUserDefaults standardUserDefaults]setValue:@"no" forKey:@"singleTap"];
}
for(UIView *subview in [cell subviews])
{
if([subview isKindOfClass:[UIButton class]])
{
[subview removeFromSuperview];
}
}
[cell.layer removeAllAnimations];
// _deleteButton.hidden = YES;
[_deleteButton removeFromSuperview];
}
return cell;
}
但它不会将按钮添加到左上角。
我想要这样的
编辑:
【问题讨论】:
-
你把这些代码放在哪里了?
-
在 cellForItemAtIndexPath 中。
-
cellForItemAtIndexPath不是进行此类自定义的最佳位置,但如果您做得对,它就可以工作。请分享更多代码,至少是您的完整方法cellForItemAtIndexPath。但是,我会选择自定义单元类。 -
检查更新问题。@HermannKlecker
标签: ios objective-c iphone ipad uicollectionviewcell