0

UITableViewがあり、18のセクションがあり、各セクションには1つのタイトルヘッダーと1つのセルがあります。各セルには、複数の画像を含む水平方向のUIScrollViewがあります。

問題は...画像とセルを正しくロードできますが、画像の量が増えてセルが上にスクロールされると、別のセルからの画像が他のセルにポップアップします...そしてすべてのレンダリングと画像すべて間違っています...セクション3または4にあるはずの画像がセクション1....などに表示されます。

データソースはJSONから読み込まれ、JSONは正しいです。画像が多くなく、セクションが5〜8しかない場合は、すべてが完全に機能します。したがって、この問題を引き起こすのはUITableViewCellの再利用であると私は信じています。

誰かが私にこれを手に入れることができますか...ありがとう..以下はUITableViewCellのコードです

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = nil;

// add a placeholder cell while waiting on table data
if ([self.datasource count] == 0 && indexPath.section == 0 && indexPath.row == 0)
{   
    UILabel *Fetch;
    static NSString *infoCell = @"infoCell";
    cell = [tableView dequeueReusableCellWithIdentifier:infoCell];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:infoCell] autorelease];   
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        CellFetch = [[[UILabel alloc] initWithFrame:CGRectMake(15.0, 120.0, 280.0, 80.0)] autorelease];
        CellFetch.tag = FETCH_TAG;
        CellFetch.font = [UIFont boldSystemFontOfSize:16.0];
        CellFetch.textColor = [UIColor grayColor];
        CellFetch.shadowColor = [UIColor whiteColor];
        CellFetch.shadowOffset = CGSizeMake(0, -0.5);
        CellFetch.textAlignment = UITextAlignmentCenter;
        [cell.contentView addSubview:CellFetch];
    } else {
        CellFetch = (UILabel *)[cell.contentView viewWithTag:FETCH_TAG];
    }
    Fetch.text = fetchStatus;
    return cell;
} else {
UIScrollView *imgScroll;
static NSString *imgCell = @"imgCell";
cell = [tableView dequeueReusableCellWithIdentifier:imgCell];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgCell] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    imgScroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 108.0)]autorelease];
    imgScroll.tag = SCROLLVIEW_TAG;
    imgScroll.showsVerticalScrollIndicator = NO;
    imgScroll.showsHorizontalScrollIndicator = NO;
} else {
    imgScroll = (UIScrollView *)[cell.contentView viewWithTag:SCROLLVIEW_TAG];
}
    NSArray *sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    //NSArray *allDistrict = [self.datasource allValues];
    NSString *key = [sorted objectAtIndex:indexPath.section];
    NSArray *img = [self.datasource valueForKey:key];

    int column = 0;
    int space = 10;
    int width = 80;
    int x = space+5.0;
    for (NSUInteger i=0; i < img.count; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        NSDictionary *Info = [img objectAtIndex:i];
        NSString *iconURL = [NSString stringWithFormat:@"%@",[Info objectForKey:@"ImgURL"]];
        button.frame = CGRectMake(x , 4, 80, 80);
        [button setImageWithURL:[NSURL URLWithString:iconURL] placeholderImage:[UIImage imageNamed:@"pholder"]];
        NSString *imgID = [Info objectForKey:@"ImgID"];
        int k = [imgID intValue];
        button.tag = k; 
        [button addTarget:self action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [imgScroll addSubview:button];


        x+=space+width;
        column++;
    }

    [imgScroll setContentSize:CGSizeMake(column*90+10, 108)];

    [cell.contentView addSubview:imgScroll];
}
// Configure the cell...
return cell;

}

4

2 に答える 2

1

セル識別子をセクションごとに一意にして、セクション間で再利用されないようにしてください。

  cell = [tableView dequeueReusableCellWithIdentifier:@"section1cell"];

  cell = [tableView dequeueReusableCellWithIdentifier:@"section2cell"];

特定のセクションのセルに画像がないが、他のセクションには画像があるという問題を見てきました。したがって、セルを再利用する場合、特にimageプロパティをnilに設定しない限り、以前にロードされた画像を取得できます。

アップデート:

これがセクション番号に関して私が話していることです

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSString *section = [NSString stringWithFormat:@"section%@cell", indexPath.section];

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:section];

    // some configuration

    return cell;
}
于 2012-06-27T18:49:29.197 に答える
0

部分的に書い [cell.contentView addSubview:imgScroll];cell == nilください。

お気に入り 、

      if (cell == nil) 
{

         cell = [[[UITableViewCell alloc] nitWithStyle:UITableViewCellStyleDefault   reuseIdentifier:imgCell] autorelease];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;

         imgScroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 108.0)]autorelease];
         imgScroll.tag = SCROLLVIEW_TAG;
         imgScroll.showsVerticalScrollIndicator = NO;
         imgScroll.showsHorizontalScrollIndicator = NO;
         [cell.contentView addSubview:imgScroll];

}
于 2012-06-27T18:48:06.127 に答える