在iOS中的滚动视图中创建自定义视图时出现问题

时间:2013-03-22 10:07:32

标签: ios objective-c uiview

我在scrollview的{​​{1}}内创建自定义视图时遇到问题。屏幕上只添加一个图像,这是最后一个图,只添加了一个标题。其余的没有出现。我需要有4张图片和4个文字。

我的自定义类名为iOS

我的自定义视图应如下所示:

enter image description here

这是在 ViewController.m

中设置scrollview的方式
mapview

MapView.h

-(void)initialiseScreen
{

    NSArray *MapGalleryArray = [[Singleton sharedManager] getMapGalleryItems];
    int yOffset = 0;
    for (int counter=0; counter < [MapGalleryArray count]; counter++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@.JPG",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Image"]];
        UIImage *MapImage = [UIImage imageNamed:imageName];

        NSString *titletext = [NSString stringWithFormat:@"%@",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Title"]];

        MapView *map = [[MapView alloc] initWithImage:MapImage label:titletext];
        [MapScrollView addSubview:map];

        yOffset +=MapScrollView.frame.size.height/MapGalleryArray count];
    }
    [MapScrollView setContentSize:CGSizeMake(MapScrollView.frame.size.width, yOffset)];
}

MapView.m

  @interface MapView : UIView
    {
        UIButton    *mapViewButton;
        UILabel     *titeLabel;
    }

    @property(nonatomic,retain)     UIButton        *mapViewButton;
    @property(nonatomic,retain)     UILabel         *titleLabel;

    -(id)initWithImage:(UIImage*)image label:(NSString*)text;

需要一些指导,我在做错误。 抱歉,这是我第一次创建自定义视图。 欢迎任何建议。

2 个答案:

答案 0 :(得分:0)

您正在滚动视图中添加相同的MapView,这就是为什么您只能看到添加的最后一个视图。增加MapView的x / y并为ur scrollView设置新的contentSize。试试以下。

for (int counter=0; counter < [MapGalleryArray count]; counter++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@.JPG",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Image"]];
        UIImage *MapImage = [UIImage imageNamed:imageName];

        NSString *titletext = [NSString stringWithFormat:@"%@",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Title"]];

        //horizontal scroll view    
        MapView *map = [[MapView alloc] initWithImage:MapImage label:titletext];
        float width = map.frame.size.width;    
         [map setFrame:CGRectmake((width*counter)+5, //x ,
             map.frame.origin.y, //y
             map.frame.size.width, //width
            map.frame.size.height)]; //height

        [MapScrollView addSubview:map];

   MapScrollView.contentSize =CGSizeMake((width+5)*10,
                                          MapScrollView.frame.size.height
               );
        yOffset +=MapScrollView.frame.size.height;
    }

答案 1 :(得分:0)

您的代码存在MapView.m

的框架设置问题

尝试使用此代码...

<强> ViewController.m

-(void)initialiseScreen
{

    NSArray *MapGalleryArray = [[Singleton sharedManager] getMapGalleryItems];
    int yOffset = 0;
    int xOffset = 0;
    for (int counter=0; counter < [MapGalleryArray count]; counter++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@.JPG",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Image"]];
        UIImage *MapImage = [UIImage imageNamed:imageName];

        NSString *titletext = [NSString stringWithFormat:@"%@",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Title"]];
        CGPoint position = CGPointMake(xOffset, yOffset);
        MapView *map = [[MapView alloc] initWithImage:MapImage label:titletext andPoint:position];
        [MapScrollView addSubview:map];

        yOffset +=MapScrollView.frame.size.height/[MapGalleryArray count];
    }
    [MapScrollView setContentSize:CGSizeMake(MapScrollView.frame.size.width, yOffset)];
}

<强> MapView.h

@interface MapView : UIView
    {
        UIButton    *mapViewButton;
        UILabel     *titeLabel;
    }

    @property(nonatomic,retain)     UIButton        *mapViewButton;
    @property(nonatomic,retain)     UILabel         *titleLabel;

    -(id)initWithImage:(UIImage*)image label:(NSString*)text andPoint:(CGPoint)point;

<强> MapView.m

-(id)initWithImage:(UIImage*)image label:(NSString*)text andPoint:(CGPoint)point
{
    self = [super init];
    if (self) {

        self.frame = CGRectMake(point.x, point.y, image.size.width/2, image.size.height/2);

        mapViewButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        [mapViewButton setImage:image forState:UIControlStateNormal];
        [mapViewButton setImage:image forState:UIControlStateHighlighted];
        [mapViewButton addTarget:self action:@selector(ImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, mapViewButton.frame.size.width+5, mapViewButton.frame.size.height)];
        [titleLabel setText:text];

        [self addSubview:mapViewButton];
        [self addSubview:titleLabel];
    }
    return self;
}

这将解决您的问题......