0

こんにちは、私は内部にいくつかのセルを持つ通常の UITableViewController を持っています。私はこのプロパティを持つセルを 1 つだけ持っています:

cell.backgroundColor = [UIColor lightGrayColor];

セルに灰色が表示されます(良い)。

問題は、スクローラーを使用するときに発生します (ウィンドウをタップして、他のセルを表示するために下に移動します)。この場合、セルのグレー色が (めちゃくちゃ) 位置から別のセルに移動します。

どうして ??

コード:

static NSString *CellIdentifier = @"セル";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

if(cell.backgroundColor == [UIColor lightGrayColor])
{
   cell.backgroundColor = [UIColor lightGrayColor];
}
else
{
   cell.backgroundColor = [UIColor clearColor];
}
switch (currentStatus) {
    case KBI_NAME:
    {
        switch (indexPath.section) {
            case 0:
            {
                if(indexPath.row == 0)
                {
                    if (currentUser.currentKBI != nil && ![currentUser.currentKBI isEqualToString:@""]) {
                        cell.textLabel.text = currentUser.currentKBI;
                    }
                    else{
                        cell.textLabel.text = @"asdf";
                    }  

                    cell.userInteractionEnabled = NO;

                    cell.backgroundColor = [UIColor lightGrayColor];
                }
                if(indexPath.row == 1)
                {
                    cell.textLabel.text = @"xyz";
                    cell.textLabel.textAlignment = UITextAlignmentCenter;
                }

                break;
            }
            case 1:
4

1 に答える 1

1

でスクロールするとUITableView、スクロールして表示されないセルが、スクロールして表示されるセルに再利用されます。それが次の行です。

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

したがって、この行がセルを返す場合、灰色の背景のセルを取得した可能性があるため、毎回背景色を設定する必要があります。

if (isGrayCell)
    cell.backgroundColor = [UIColor lightGrayColor];
else
    cell.backgroundColor = [UIColor clearColor];

アップデート

背景色を設定するコードは意味がありません。リサイクルされたセルの背景が灰色の場合は、別の色が必要な場合でも再び灰色に設定します。次のようなものは次のようになります。

if(currentStatus == KBI_NAME && indexPath.row == 0)
{
   cell.backgroundColor = [UIColor lightGrayColor];
}
else
{
   cell.backgroundColor = [UIColor clearColor];
}

更新 2

毎回セルを単純に初期化すると、おそらくはるかに簡単になります。

cell.backgroundColor = [UIColor clearColor];

灰色の背景が必要な場合は、後で色を再び灰色に変更します。

于 2012-05-27T10:49:49.127 に答える