2017-07-25 5 views
0

問題:私のカスタムUITableViewCellには追加のUILabelと追加のUIViewが含まれています。 私のコードでは、このオブジェクトの情報がないときにこれらの余分なUILabelとUIViewを削除します。 しかし、ユーザーがUITableViewをスクロールするたびに、これらのラベルは隠されていますが、これらのセルには追加の情報があり、別のセルに表示されます。UITableViewCellはスクロール後にコンテンツを変更します

あなたはスーパーからラベルを削除しますが、あなたのセルのサブビューとしてそれを再追加していない:私は問題を見

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

NBSnackTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"]; 


NSDictionary *softdrinkDict = [_allSoftdrinksArray objectAtIndex:indexPath.row]; 

cell.snackNameLabel.text = [softdrinkDict valueForKey:@"name"]; 
cell.snackPriceLabel.text = [NSString stringWithFormat:@"%@ - %@", [softdrinkDict valueForKey:@"size"], [softdrinkDict valueForKey:@"preis"]]; 


    if ([softdrinkDict valueForKey:@"info"]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryDetailButton; 
     cell.tintColor = [NBColor primaryTextColor]; 
     cell.snackInfoLabel.text = [softdrinkDict valueForKey:@"info"]; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [cell.snackInfoLabel removeFromSuperview]; 
     [cell.snackInfoView removeFromSuperview]; 
    } 

    if ([softdrinkDict valueForKey:@"zusatzstoffe"]) 
    { 
     cell.snackZusatzstoffLabel.text = [softdrinkDict valueForKey:@"zusatzstoffe"]; 
    } 
    else 
    { 
     [cell.snackZusatzstoffLabel removeFromSuperview]; 
    } 

[cell layoutContentWithRect:cell.bounds]; 

return cell;} 
+0

だから、セルに表示されるべき内容ではなく、別のセルに表示されますか? –

答えて

1

は、ここに私のコードです。

ラベルでremoveFromSuperviewを呼び出す代わりに、テキストを@ ""に設定するか、表示するデータがあるときにセルのcontentViewにラベルを追加することができます。

例:

if ([softdrinkDict valueForKey:@"zusatzstoffe"]) 
{ 
    if (![cell.snackZusatzstoffLabel isDescendantOfView:cell.contentView]) { 
     [cell.contentView addSubview:cell.snackZusatzstoffLabel]; 
    } 
    cell.snackZusatzstoffLabel.text = [softdrinkDict valueForKey:@"zusatzstoffe"]; 
} 
else 
{ 
    [cell.snackZusatzstoffLabel removeFromSuperview]; 
} 
+0

それはうまく動作します!あなたのソリューションをありがとう!それは私をたくさん助けました。 –

+0

@ profidash_98大歓迎です! –

関連する問題