我的gridcell重叠了我的imageview

时间:2013-12-09 12:55:52

标签: ios iphone objective-c

我想用静态图像视图覆盖网格视图的一个单元格但是当我添加图像视图时,它会出现在单元格下方,但我希望它在单元格上方,以便细胞图像被静态图像视图覆盖,静态图像视图可以在细胞移动时移动。在我的代码中,我的imageview正在吹动单元格,但我想要一个固定的图像始终只是网格单元的顶部。当我移动单元格时,我的imageview不会移动

DWExampleGridViewController.h
#import "DWGridController.h"

@interface DWExampleGridViewController : DWGridViewController
{

}
@property (strong, nonatomic) IBOutlet UIImageView *iv;
@end


    DWExampleGridViewController.m


    #import "DWExampleGridViewController.h"
    #import "DWExampleGridViewCell.h"
    #import "DWExampleGridViewController.h"
    #import "DWGridViewController.h"
    @interface DWExampleGridViewController ()

    @end

    @implementation DWExampleGridViewController
    @synthesize iv;
    - (id)init
    {
        self = [super init];
        if (self)
        {

            for(int row = 0; row < 9; row++)
            {
                for(int col = 0; col < 9; col++)
                {

                        DWExampleGridViewCell *cell = [[DWExampleGridViewCell alloc] init];
                        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d-%d.jpeg",row,col]];

                        iv = [[UIImageView alloc] initWithImage:image];
                        [iv setContentMode:UIViewContentModeScaleAspectFill];
                     iv.clipsToBounds = YES;

                        [iv setTranslatesAutoresizingMaskIntoConstraints:NO];
                        //[cell addSubview:iv];
                    //[self.view insertSubview:iv atIndex:0];

                        [cell insertSubview:iv atIndex:0];

                        [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[iv]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(iv)]];
                        [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[iv]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(iv)]];

                        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                        [dict setObject:cell forKey:@"Cell"];
                        [dict setObject:[NSNumber numberWithInt:row] forKey:@"Row"];
                        [dict setObject:[NSNumber numberWithInt:col] forKey:@"Column"];


                        if(image)
                            [dict setObject:image forKey:@"Image"];
                        [self.cells addObject:dict];
                }
            }


        }
         [self image];

        return self;
    }

    - (void)viewDidLoad
    {


    }
    -(void)image
    {
        DWExampleGridViewCell *cell = [[DWExampleGridViewCell alloc] init];
        UIImageView *v = [[UIImageView alloc] init];
        v.frame=CGRectMake(108,155, 105, 160);
        v.image=[UIImage imageNamed:@"4.jpeg"];
        [v setContentMode:UIViewContentModeScaleAspectFill];
        v.clipsToBounds = YES;
        //DWExampleGridViewController *grid = [[DWExampleGridViewController alloc]init];
        //[self.view insertSubview:v aboveSubview:grid.iv];
        //[self.view insertSubview:v atIndex:2];
        //[cell insertSubview:v atIndex:1];
        //[cell insertSubview:v aboveSubview:iv];
        [[[[UIApplication sharedApplication] delegate] window] insertSubview:v aboveSubview:cell];
         [self.view bringSubviewToFront:self.gridView];

    }



    #pragma mark - GridView datasource
    -(NSInteger)numberOfColumnsInGridView:(DWGridView *)gridView
    {
        return 8;
    }

    -(NSInteger)numberOfRowsInGridView:(DWGridView *)gridView
    {
        return 8;
    }

    -(NSInteger)numberOfVisibleRowsInGridView:(DWGridView *)gridView
    {
        return 3;
    }

    -(NSInteger)numberOfVisibleColumnsInGridView:(DWGridView *)gridView
    {
        return 3;
    }


    #pragma mark - GridView delegate
    -(void)gridView:(DWGridView *)gridView didSelectCell:(DWGridViewCell *)cell atPosition:(DWPosition)position
    {
        NSDictionary *cellDictionary = [self cellDictionaryAtPosition:position];
        UIImage *image = [cellDictionary objectForKey:@"Image"];

        UIButton *button = [[UIButton alloc] init];
        button.translatesAutoresizingMaskIntoConstraints = NO;
        [button setBackgroundImage:image forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchDown];

        UIViewController *contr = [[UIViewController alloc] init];
        [contr.view addSubview:button];

        [contr.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[button]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]];
        [contr.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[button]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]];

        [self presentViewController:contr animated:YES completion:nil];
    }

    -(void)buttonTapped
    {

        [[self presentedViewController] dismissViewControllerAnimated:YES completion:nil];
    }
    @end

2 个答案:

答案 0 :(得分:0)

  1. 制作自定义单元格
  2. 使用addSubview:
  3. 将静态图片添加到单元格contentView
  4. 每当您希望图片位于最顶层的电话[cell.contentView bringSubviewToFront:imageView]
  5. 如果这没有帮助 - 发布错误的屏幕截图以及您希望它如何。

答案 1 :(得分:0)

  1. 使视图具有与单元格相同的属性。
  2. 在关键窗口上添加所需位置的视图。
  3. 永远不要忘记viewWillDisappear委托上的[view removeFromSuperview]。