首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于iCarousel和labels

关于iCarousel和labels
EN

Stack Overflow用户
提问于 2015-09-07 05:56:38
回答 2查看 293关注 0票数 1

我有两个标签。

名为" label“的第一个标签被放置在轮播中的每个视图中。标签的字符串/文本是视图的索引。

代码语言:javascript
复制
label.text = [[items1 objectAtIndex:index] stringValue];

我还有第二个标签(在carousel之外),名为"outsideLabel“。我希望outsideLabel的字符串/文本也是视图的索引(视图总是在carousel的前面)。

代码语言:javascript
复制
outsideLabel.text = [[items1 objectAtIndex:index] stringValue];

不知何故,我做错了,不知道该如何编码,以便在outsideLabel的字符串/文本中显示正确的数字(视图总是在前面)。代码在某种程度上显示了正确的数字,但在旋转木马中向后滚动时会变得混乱。carouseltype为timeMachine。

我当前的代码:

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

发布于 2015-09-20 12:45:45

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

代码语言:javascript
复制
#define INSIDE_LABEL_TAG  1
#define OUTSIDE_LABEL_TAG 2

这要安全得多,因为它不依赖于视图的结构,就像你的代码一样,你得到的是最后一个视图:

代码语言:javascript
复制
label = [[view subviews] lastObject];

尝试在该块中初始化outsideLabel,并使用标记。初始化中使用的模式与用于UITableViewDataSource委托中UITableView单元格的子视图的模式相同:

代码语言:javascript
复制
(UITableViewCell * _Nonnull)tableView:(UITableView * _Nonnull)tableView
                cellForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath

下面是一些伪代码,它显示了我将在何处使用标记并初始化outsideLabel

代码语言:javascript
复制
- (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;
}
票数 2
EN

Stack Overflow用户

发布于 2015-09-07 07:36:39

在我看来你想要“时光机”风格的旋转木马。我没看到你的代码设置了carousel类型。您不需要设置转盘类型吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32428697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档