2012-05-16 23 views
0

我试图在`UITableView'的每个单元中实现UIScrollView。我基于Apple的自定义Scrollview项目编写了代码:https://developer.apple.com/library/ios/#samplecode/Scrolling/Listings/ReadMe_txt.htmlUIScrollView在cellForRowAtIndex路径不工作的自定义表单元格

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

一切正常,直到我向下滚动到第4个表单元格。该单元格中的UIScrollView与第一个单元格具有相同的确切标签。在前3个单元之后,相同的前3个标签或前3个scrollView继续重复。奇怪的部分是,当它在设置后登录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

试铸字细胞=(Customcell *)的tableView dequeueReusableCellWithIdentifier:CellIdentifier]; –

+0

你是什么意思铸字是什么popularlinktitlearray? popularLinkTitleArray包含附加到标签的字符串。我没有在上面的代码中定义它,但是当我使用'NSLog'数组时它正常工作。 – mnort9

回答

0

您不应该将标签作为子视图添加到cellForRowAtIndexPath中的popularLinkScrollView中。如果标签的数量必须是动态的,那么在cellForRowAtIndexPath中的for循环之前尝试下面的代码。

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

好的,那是有效的。我只需要将for语句改为:'for(UIView * subView in cell.popularLinkScrollView.subviews)''。谢谢。顺便说一句,为什么你建议不要在cellForRowAtIndexPath中添加标签?我应该在哪里添加它? – mnort9

+0

如果这些标签的数量已知且始终固定,则应将其放入CustomCell本身的实现中。如果另一方面它不是固定的。您可以尝试两种方法1)如果您知道标签和标签的最大数量是按特定顺序排列的,请在CustomCell实现中添加最大标签,并在cellForRowAtIndexPath中隐藏/取消隐藏标签。 2)另一种方法如上所述。 –

+0

它是固定的,但我不太明白如何将它放在我的CustomCell实现中。我是否在CustomCell .m中创建一个方法并在ViewController'cellForRowAtIndexPath'中调用该方法? – mnort9

相关问题