【问题标题】:UIScrollView in custom table cell at cellForRowAtIndex path not working在 cellForRowAtIndex 路径的自定义表格单元格中的 UIScrollView 不起作用
【发布时间】:2012-05-16 19:30:54
【问题描述】:

我正在尝试在“UITableView”的每个单元格中实现UIScrollView。我基于 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;
}

【问题讨论】:

  • 尝试类型转换 cell = (Customcell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];什么是流行的linktitlearray
  • 类型转换是什么意思? popularLinkTitleArray 包括附加到标签的字符串。我没有在上面的代码中定义它,但是当我 NSLog 数组时它可以正常工作。

标签: iphone objective-c ios uitableview uiscrollview


【解决方案1】:

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

for (UIView* subView in cell.popularLinkScrollView.subviews)
    [subView removeFromSuperview];

【讨论】:

  • 好的,这行得通。我只需要将 for 语句更改为:for (UIView* subView in cell.popularLinkScrollView.subviews)。谢谢你。顺便问一下,为什么不建议在 cellForRowAtIndexPath 中添加标签?我应该在哪里添加它?
  • 如果此类标签的数量已知且始终固定,则应将其放入 CustomCell 本身的实现中。另一方面,如果它不固定。您可以尝试两种方法 1) 如果您知道标签的最大数量和标签按特定顺序排列,则在 CustomCell 实现中添加最大标签并在 cellForRowAtIndexPath 中隐藏/取消隐藏标签。 2)另一种方法如上所述。
  • 它是固定的,但我不太明白如何将它放在我的 CustomCell 实现中。我是否在 CustomCell .m 中创建一个方法并在 ViewController cellForRowAtIndexPath 中调用该方法?
  • 您可以为您的 CustomCell 提供一个 xib 文件。如果您不知道如何通过 xib 文件创建自定义单元格,我建议您浏览名为“AdvancedTableViewCells”的应用程序的 Apple 示例代码库,其中解释了使用自定义单元格的不同方面。
  • 感谢您的帮助 Mithlesh。我看了一下 AdvancedTableCells。我知道我可以将我的标签创建代码放在我的实现中的一个方法中。我不明白的是如何以及何时调用此方法。我可以在 CustomCell.m 中使用 viewDidLoad 吗?我是否在 CellForRowAtIndexPath 中调用 CustomCell 方法?如果有,怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 2021-09-17
相关资源
最近更新 更多