我正在尝试在`UITableView'的每个单元格中实现UIScrollView
。我根据Apple的自定义Scrollview项目编写了我的代码:https://developer.apple.com/library/ios/#samplecode/Scrolling/Listings/ReadMe_txt.html
每个表格单元格的UIScrollView
滚动浏览一组标签,这些标签在cellForRowAtIndexPath
方法中初始化。初始化后,每个标签的文本都设置为等于数组中的字符串。
一切正常,直到我向下滚动到第4个表格单元格。此单元格中的UIScrollView
与第一个单元格具有完全相同的标签。相同的前3组标签或前3个scrollView在前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;
}
答案 0 :(得分:0)
您不应将标签作为子视图添加到cellForRowAtIndexPath中的popularLinkScrollView。如果标签的数量需要是动态的,请在cellForRowAtIndexPath中的for循环之前尝试此代码。
for (UIView* subView in cell.popularLinkScrollView.subviews)
[subView removeFromSuperview];