I want to add the Button to the top left corner of the cell.I have tried but i always added inside the collection view cell but not on the cell so please tell me how can i o this?
I have tried this.
- (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;
}
But it does not add the button to the top left.
I want like this
发送数据修改
答案 0 :(得分:1)
您在Main.storyBoard中的设计如下:
现在,在自定义单元格中创建Button的IBOutlet。 并在 cellForItemAtIndexPath 方法中设置此按钮的属性,如:
// Note that $timeout is now injected to the directive
angular.module('myApp', []).directive('formInput', function ($timeout) {
return {
restrict: 'A',
//require: '^form',
link: function (scope, element, attributes, form) {
$timeout(function() {
var input, name;
input = element[0].querySelector('input, textarea, select');
name = input.getAttribute('name');
alert(name);
}, 0);
// ...
// do stuff with input, name, form etc.
}
};
});
并创建 btnDeleteFolderTapped 方法:
[cell.btnDelete setTag:indexPath.row];
[cell.btnDelete addTarget:self action:@selector(btnDeleteFolderTapped:) forControlEvents:UIControlEventTouchUpInside];
表示单元格之间的空格,将下面的代码放在.m文件中:
-(void)btnDeleteFolderTapped:(id)sender
{
// handle your stuff here
}
然后转到Main.storyboard,单击collectionView设置属性,如:
答案 1 :(得分:0)
因为您将按钮添加为单元格的子视图,当然它将在单元格中添加按钮而不是。例如,您的图标大小为60x60,单元格大小为80x80。只需在该单元格上添加不可见(我的意思是backgroundColor
清晰)UIView
,其大小与单元格80x80相同,并添加图标作为该视图的子视图,并为此框架CGRectMake(10, 10, 60, 60)
添加。最后,在左上角添加图标作为该视图的子视图,如CGRectMake(0, 0, delButtonSize, delButtonSize)
。您的UI层次结构应该是这样的;
UICollectionViewCell - > UIView - >删除按钮&应用程序图标
答案 2 :(得分:0)
1.拍摄你必须在细胞中显示的图像小于细胞大小并将其对齐到右下角。