我有两个标签。
名为" label“的第一个标签被放置在轮播中的每个视图中。标签的字符串/文本是视图的索引。
label.text = [[items1 objectAtIndex:index] stringValue];我还有第二个标签(在carousel之外),名为"outsideLabel“。我希望outsideLabel的字符串/文本也是视图的索引(视图总是在carousel的前面)。
outsideLabel.text = [[items1 objectAtIndex:index] stringValue];不知何故,我做错了,不知道该如何编码,以便在outsideLabel的字符串/文本中显示正确的数字(视图总是在前面)。代码在某种程度上显示了正确的数字,但在旋转木马中向后滚动时会变得混乱。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;
}发布于 2015-09-20 12:45:45
从您提供的代码来看,您似乎没有在正确的位置初始化outsideLabel。为安全起见,您应该在检查视图是否为nil的块中初始化所有子视图。另一个安全的约定是为所有的子视图分配标记,这样以后就可以从重用的视图中检索它们,如下面的代码所示。为了便于参考和避免错误,我在实现文件的顶部定义了这些标记的常量,如下所示:
#define INSIDE_LABEL_TAG 1
#define OUTSIDE_LABEL_TAG 2这要安全得多,因为它不依赖于视图的结构,就像你的代码一样,你得到的是最后一个视图:
label = [[view subviews] lastObject];尝试在该块中初始化outsideLabel,并使用标记。初始化中使用的模式与用于UITableViewDataSource委托中UITableView单元格的子视图的模式相同:
(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;
}发布于 2015-09-07 07:36:39
在我看来你想要“时光机”风格的旋转木马。我没看到你的代码设置了carousel类型。您不需要设置转盘类型吗?
https://stackoverflow.com/questions/32428697
复制相似问题