gpt4 book ai didi

ios - 关于 iCarousel 和标签 iOS

转载 作者:行者123 更新时间:2023-11-29 10:25:41 24 4
gpt4 key购买 nike

我有 2 个标签。

第一个标签,名为“标签”,放置在轮播中的每个 View 内。标签的字符串/文本是 View 的索引。

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

我还有第二个标签(在旋转木马之外)名为“outsideLabel”。我希望 outsideLabel 的字符串/文本也成为 View 的索引( View 始终位于旋转木马的前面)。

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

不知何故我做错了,想知道我应该如何编码才能在 outsideLabel 的字符串/文本中显示正确的数字( View 总是在前面)。该代码在某种程度上显示了正确的数字,但在旋转木马中向后滚动时变得困惑。 carouseltype 是 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;
}

最佳答案

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

#define INSIDE_LABEL_TAG  1
#define OUTSIDE_LABEL_TAG 2

这更安全,因为它不依赖于 View 的结构,就像您的代码一样,您在其中获得最后一个 View :

label = [[view subviews] lastObject];

尝试在该 block 内初始化 outsideLabel,并使用标签。初始化中使用的模式与用于 UITableViewDataSource 委托(delegate)中的 UITableView 单元格 subview 的模式相同:

(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;
}

关于ios - 关于 iCarousel 和标签 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32428697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com