0

我正在尝试UIScrollView在“UITableView”的每个单元格中实现一个。我基于 Apple 的自定义 Scrollview 项目编写了我的代码:https ://developer.apple.com/library/ios/#samplecode/Scrolling/Listings/ReadMe_txt.html

每个表格单元格UIScrollView滚动浏览一组标签,这些标签在cellForRowAtIndexPath方法中被初始化。每个标签的文本在初始化后设置为等于数组中的字符串。

一切正常,直到我向下滚动到第 4 个表格单元格。此UIScrollView单元格中的 与第一个单元格具有相同的标签。相同的前 3 组标签或前 3 个滚动视图在前 3 个单元格之后不断重复。奇怪的是,当我在label.text设置后记录它时,输出显示label.text了相应单元格中应该显示的正确内容。

- (void)layoutScrollLabels: (float)arrayCount
{
UIView *view = nil;
NSArray *subviews = [cell.popularLinkScrollView subviews];

// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
    if ([view isKindOfClass:[UILabel class]] && view.tag >= 0)
    {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        view.frame = frame;

        curXLoc += (kScrollObjWidth);
    }
}

[cell.popularLinkScrollView setContentSize:CGSizeMake((arrayCount * kScrollObjWidth),     [cell.popularLinkScrollView bounds].size.height)];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Custom Cell";

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSDictionary *dictionary = [parseDataArray objectAtIndex:indexPath.row];
NSArray *popularLinkTitleArray = [dictionary objectForKey:@"popularLinkTitleArray"];

cell.popularLinkScrollView.clipsToBounds = YES;

kScrollObjHeight = cell.popularLinkScrollView.frame.size.height;
kScrollObjWidth = cell.popularLinkScrollView.frame.size.width;

NSUInteger i;
for (i = 0; i < popularLinkTitleArray.count; i++)
{
    NSString *string = [NSString stringWithFormat:@"%@", [popularLinkTitleArray objectAtIndex: i]];
    UILabel *label = [[UILabel alloc] init];
    label.text = [NSString stringWithFormat:@"%@", string];
    label.backgroundColor = [UIColor clearColor];
    label.numberOfLines = 5;
    NSLog(@"%@", label.text);

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    CGRect rect = label.frame;
    rect.size.height = kScrollObjHeight;
    rect.size.width = kScrollObjWidth;
    label.frame = rect;
    label.tag = i;  // tag our images for later use when we place them in serial fashion
    [cell.popularLinkScrollView addSubview:label];
}

[self layoutScrollLabels:popularLinkTitleArray.count];
}
return cell;
}
4

1 回答 1

0

您不应将标签作为子视图添加到 cellForRowAtIndexPath 中的 PopularLinkScrollView。如果标签计数需要是动态的,请在 cellForRowAtIndexPath 中的 for 循环之前尝试此代码。

for (UIView* subView in cell.popularLinkScrollView.subviews)
    [subView removeFromSuperview];
于 2012-05-16T19:56:54.777 回答