0

UIScrollView`UITableView'の各セルにを実装しようとしています。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;
}
4

1 に答える 1

0

cellForRowAtIndexPath で、popularLinkScrollView にサブビューとしてラベルを追加しないでください。ラベルの数を動的にする必要がある場合は、cellForRowAtIndexPath の for ループの前にこのコードを試してください。

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