0

ここでは、上下に2〜3回スクロールした後のテーブルビューで、すべてのセルに画像を追加します。コードは次のとおりです。

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

HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:@"HomeCell"];
if (cell == nil) {
    cell = [[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeCell"] ;
}

VenueDC *venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row];
cell.lblName.text = venueObj.name;
if ([venueObj.imagesArray count] > 0) {
    [cell.ivVenue setImage:venueObj.ivVenue];
    [cell.ivVenue setHidden:NO];
    cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height);
}
venueObj = nil;


return  cell;
}

ここで何が起こっているのか分かりますか?

画像は1つのオブジェクトにのみ存在し、最初の読み込み時に1つのセルに画像が表示されますが、スクロールすると、他のセルにも画像が表示され始めます。

4

6 に答える 6

3

このように解決しました。最初にlblNameフレームを元の位置に設定したため、セルが使用されるたびに位置がリセットされました

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

HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:@"HomeCell"];

if (cell == nil) {

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil];

    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell =  (HomeCell *) currentObject;
            break;
        }
    }
}

VenueDC *venueObj = nil;
venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row];
cell.lblName.text = venueObj.name;
[cell.ivVenue setHidden:YES];
cell.lblName.frame = CGRectMake(20 , 19, 250, 20);
if (venueObj.ivVenue) {
    [cell.ivVenue setImage:venueObj.ivVenue];
    [cell.ivVenue setHidden:NO];
    cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height);
} 
return  cell;
}
于 2013-01-11T13:58:05.757 に答える
2

UITableViewCells再利用されます。これがdequeueReusableCellWithIdentifier: メソッドの機能です。メモリを節約するためUITableViewに、特定の時点で確認できる限り多くのセルのみをインスタンス化し、それらを再利用し続けます。このため、再利用する前に常にセルをクリーンアップする必要があります。つまり、すべてのコンテンツを にリセットしnilます。

あなたの場合のようにカスタムテーブルビューを使用している場合はHomeCell、 -(void)prepareForReuseをオーバーライドし、そこでイメージを nil に設定します。

于 2013-01-11T13:33:45.150 に答える
1

セルが再利用されているために発生しているので、次のコードを記述します。

HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil];
        
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (HomeCell *) currentObject;
                break;
            }
        }
    }
于 2013-01-11T13:39:13.777 に答える
0

1行だけ追加する必要がありますcellForRowAtIndexPath

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];

そして変更

HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
于 2013-01-11T13:38:35.640 に答える
0

else次のステートメントが必要です。

else {
venueObj = nil;
}
于 2013-01-11T13:32:42.033 に答える
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:@"HomeCell"];
if (cell == nil) {
    cell = [[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeCell"] ;
}

VenueDC *venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row];
cell.lblName.text = venueObj.name;
    [cell.ivVenue setImage:nil];

if ([venueObj.imagesArray count] > 0) {
    [cell.ivVenue setImage:venueObj.ivVenue];
    [cell.ivVenue setHidden:NO];
    cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height);
}
venueObj = nil;


return  cell;
}
于 2013-01-11T13:35:02.677 に答える