2

私はホテルを表示し、それらが好きかどうかを示すテーブルビューを持っています。ホステルがお気に入りなら、ホステルの列アクセサリービューにお気に入りの画像を配置しました。しかし、画像はターゲットのホステルだけで表示されたわけではありません。tableViewの他のセルにランダムに配置しました。コードは次のとおりです。

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 50) reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...
        //First get the dictionary object
        NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
        NSArray *array = [dictionary objectForKey:@"Countries"];
        NSString *cellValue = [array objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;

        if (![favorites count] == 0) {
            if ([favorites containsObject:[array objectAtIndex:indexPath.row]]) {
                NSUInteger indexx = [favorites indexOfObject:cellValue];
                if ([[favorites objectAtIndex:indexx] isEqual:cellValue]) {

                    // here is the image view

                    UIImageView *imageView = [[UIImageView alloc] init];
                    [imageView setImage:[UIImage imageNamed:@"small_ItsMyFavorite.png"]];
                    imageView.frame = CGRectMake(276, 0, 50, 50);
                    [cell addSubview:imageView];
                    [imageView release];
                }
            }

        }
    }
}

return cell;
}

私は検索して検索しています、問題は何ですか?

4

3 に答える 3

3

UITableViewはセルを再利用しているため、デザインにいくつかの問題があります。

UIImageViewをセルに追加すると、再利用されている場合でも、たとえば50行あり、下にスクロールすると、セルが再利用されるたびに、UIImageViewがセルに追加されます。そして、メモリをフラッシュします。

UIImageViewを追加するUITableViewCellをサブクラス化することをお勧めします。UIImageViewの画像をnilに設定するprepareForReuseメソッドをオーバーライドすることを忘れないでください

于 2012-07-21T20:51:31.980 に答える
2

オブジェクトはリサイクルされるため、ホステルがお気に入りではない場合は、画像をクリアするためにブランチをUITableViewCell追加する必要があります。else現在お持ちのように、お気に入りを一度表示するために使用されたセルには、永久に画像が含まれます。

if ([[favorites objectAtIndex:indexx] isEqual:cellValue]) {
    // here is the image view
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView setImage:[UIImage imageNamed:@"small_ItsMyFavorite.png"]];
    imageView.frame = CGRectMake(276, 0, 50, 50);
    [cell addSubview:imageView];
    [imageView release];
} else {
    // Find the UIImageView in the subviews of your cell,
    // and remove it from superview.
}
于 2012-07-21T20:43:08.180 に答える
0

あなたはこのようなことをしなければなりません

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 50) reuseIdentifier:CellIdentifier] autorelease];
}
else 
{
    [[cell.contentView viewWithTag:999] removeFromSuperView];
}

// Set up the cell...
    //First get the dictionary object
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    if (![favorites count] == 0) {
        if ([favorites containsObject:[array objectAtIndex:indexPath.row]]) {
            NSUInteger indexx = [favorites indexOfObject:cellValue];
            if ([[favorites objectAtIndex:indexx] isEqual:cellValue]) {

                // here is the image view

                UIImageView *imageView = [[UIImageView alloc] init];
                [imageView setImage:[UIImage imageNamed:@"small_ItsMyFavorite.png"]];
                imageView.frame = CGRectMake(276, 0, 50, 50);
                imageView.tag = 999; // for example 
                [cell addSubview:imageView];
                [imageView release];
            }
        }

    }
}
}

return cell;

}
于 2012-07-21T23:29:50.567 に答える