关于iCarousel和iOS

关于iCarousel和iOS,ios,icarousel,Ios,Icarousel,我有两个标签 第一个名为“标签”的标签放置在旋转木马内的每个视图中。标签的字符串/文本是视图的索引 label.text = [[items1 objectAtIndex:index] stringValue]; 我还有第二个标签(在旋转木马外面),名为“outsideLabel”。 我希望outsideLabel的字符串/文本也成为视图的索引(视图始终位于旋转木马的前面) 不知怎的,我做错了,不知道该如何编码,以便在outsideLabel的字符串/文本中显示正确的数字(视图总是在前面)。代

我有两个标签

第一个名为“标签”的标签放置在旋转木马内的每个视图中。标签的字符串/文本是视图的索引

label.text = [[items1 objectAtIndex:index] stringValue];
我还有第二个标签(在旋转木马外面),名为“outsideLabel”。 我希望outsideLabel的字符串/文本也成为视图的索引(视图始终位于旋转木马的前面)

不知怎的,我做错了,不知道该如何编码,以便在outsideLabel的字符串/文本中显示正确的数字(视图总是在前面)。代码显示了正确的数字,但在旋转木马中向后滚动时会出错。转盘类型是timeMachine

我当前的代码:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{   
    //create new view if no view is available for recycling
    if (view == nil)
    {
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];

        view.contentMode = UIViewContentModeCenter;
        label = [[UILabel alloc] initWithFrame:view.bounds];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor whiteColor];

        if (carousel == carousel1)
        {

        CGRect test = CGRectMake(10, 10, 20, 20);
        self.label.frame = test;

         }
        else {
            CGRect test = CGRectMake(50, 40, 40, 40);
            self.label.frame = test;

            }
        [view addSubview:label];

    }
    else
    {
        label = [[view subviews] lastObject];
    }

    if (carousel == carousel1)
    {
        //items in this array are numbers
        outsideLabel.text = [[items1 objectAtIndex:index] stringValue];

        label.text = [[items1 objectAtIndex:index] stringValue];

         ((UIImageView *)view).image = [UIImage imageNamed:[view1background objectAtIndex:index]];

    }
    else
    {

       //not relevant....
    }

    return view;
}

在我看来,你想要的是“时光机器”式的旋转木马。我没有看到你的代码在任何地方设置旋转木马类型。您不需要设置转盘类型吗?

根据您提供的代码,您似乎没有在正确的位置初始化
外部标签。为了安全起见,您应该在检查视图是否为
nil
的块内初始化所有子视图。另一个安全约定是将标记分配给所有子视图,以便以后可以从重用的视图中检索它们,如下面的代码所示。为了便于参考和避免错误,我在实现文件的顶部为这些标记定义了常量,如下所示:

#define INSIDE_LABEL_TAG  1
#define OUTSIDE_LABEL_TAG 2
这更安全,因为它不依赖于视图的结构,就像您的代码一样,您可以在代码中获得最后一个视图:

label = [[view subviews] lastObject];
尝试在该块内部初始化
外部标签
,并使用标记。初始化中使用的模式与委托中
UITableViewDataSource
单元格的子视图中使用的模式相同:

(UITableViewCell * _Nonnull)tableView:(UITableView * _Nonnull)tableView
                cellForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
下面是一些伪代码,显示了我将在何处使用标记并初始化
outsideLabel

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    //create new view if no view is available for recycling
    if (view == nil)
    {
        //Configure the view
        ...

        /*  Initialize views for all carousels  */

        //Initialize the insideLabel and set its tag
        ...
        insideLabel.tag = INSIDE_LABEL_TAG;

        //Initialize the outsideLabel and set its tag
        ...
        outsideLabel.tag = OUTSIDE_LABEL_TAG;

        if (carousel == carousel1)
        {
            //Do any carousel-specific configurations
        }

        //Add all subviews initialized in this block
        [view addSubview:label];
        [view addSubview:outsideLabel];
    }
    else
    {
        //Get the subviews from an existing view
        insideLabel = (UILabel *)[view viewWithTag:INSIDE_LABEL_TAG];
        outsideLabel = (UILabel *)[view viewWithTag:OUTSIDE_LABEL_TAG];
    }

    if (carousel == carousel1)
    {
        //Set the values for each subview
    } else {
       //Other carousels...
    }

    return view;
}

我正在viewDidLoad方法中设置类型。是的,它已经是时间机器了。我仍然只看到一张卡片而不是几张。你看过iCarousel演示应用程序了吗?它们包括时间机器外观的工作演示。(我已经有一段时间没有建立一个新的iCarousel了,所以我记不清上面的细节。)你试过iCarousel的委托方法吗--(void)carouselCurrentItemIndexDidChange:(iCarousel*)carousel?尝试在此处设置值。。希望这能对你有所帮助
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    //create new view if no view is available for recycling
    if (view == nil)
    {
        //Configure the view
        ...

        /*  Initialize views for all carousels  */

        //Initialize the insideLabel and set its tag
        ...
        insideLabel.tag = INSIDE_LABEL_TAG;

        //Initialize the outsideLabel and set its tag
        ...
        outsideLabel.tag = OUTSIDE_LABEL_TAG;

        if (carousel == carousel1)
        {
            //Do any carousel-specific configurations
        }

        //Add all subviews initialized in this block
        [view addSubview:label];
        [view addSubview:outsideLabel];
    }
    else
    {
        //Get the subviews from an existing view
        insideLabel = (UILabel *)[view viewWithTag:INSIDE_LABEL_TAG];
        outsideLabel = (UILabel *)[view viewWithTag:OUTSIDE_LABEL_TAG];
    }

    if (carousel == carousel1)
    {
        //Set the values for each subview
    } else {
       //Other carousels...
    }

    return view;
}