0

viewDidAppear メソッド アクセスでテーブルの行に対して reloadData を呼び出そうとしています。ただし、セルの値が更新されていないため、すべてが想定どおりにアクセスされているように見えるため、その理由がわかりません。さらに奇妙なことに、実際には 1 つの行が更新されますが、他の行は更新されません。

これが私のコードです...

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{     
  // Set up the cell...
  static NSString *CellWithIdentifier = @"Cell";
  UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
  NSLog(@"generating cell contents");
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [_tableGroup.options objectAtIndex:rowcount];
    rowcount++;

    //label for currently selected/saved setting
    _currentSetting = [[UILabel alloc] initWithFrame:CGRectMake(160, 8, 115, 25)];
    [_currentSetting setFont:[UIFont systemFontOfSize:14]];
    _currentSetting.backgroundColor = [UIColor clearColor];
    _currentSetting.textColor = [UIColor blueColor];
    _currentSetting.textAlignment = NSTextAlignmentRight;


    [cell.contentView addSubview:_currentSetting];
    NSLog(@"added new label to cell");
}

//depending on the setting, set the label in the cell to what is currently selected
if (indexPath.section == 1 && indexPath.row == 0) {
    _currentSetting.text = [NSString stringWithFormat:@"%@ %@",[settings.mapDistance stringValue], NSLocalizedString(@"MILES_IDENTIFIER", nil)];
    NSLog(@"setting map distance label: %@", settings.mapDistance);
}
else if(indexPath.section == 1 && indexPath.row == 1)
{
    _currentSetting.text = [NSString stringWithFormat:@"%@ %@",[settings.maxCustomers stringValue], NSLocalizedString(@"ITEMS_IDENTIFIER", nil)];
    NSLog(@"setting max customers: %@", settings.maxCustomers);
}
else if(indexPath.section == 2)
{
    _currentSetting.text = [NSString stringWithFormat:@"%@ %@",[settings.maxProducts stringValue], NSLocalizedString(@"ITEMS_IDENTIFIER", nil)];
    NSLog(@"setting max products: %@", settings.maxProducts);
}

return cell;
}

このコードに基づいて、NSLOGS でこの出力を取得します。

これは、ビューが作成されたときのセルの最初の実行です。4 つのセルを生成し、各セルにラベルを付け、そのうちの 3 つのラベルに値を入れます。

 generating cell contents
 added new label to cell
 generating cell contents
 added new label to cell
 setting map distance: 15
 generating cell contents
 added new label to cell
 setting max customers: 250
 generating cell contents
 added new label to cell
 setting max products: 150

この時点で、行をクリックして別の画面に移動し、戻ってきました。ご覧のとおり、マップの距離が異なります。再読み込みプロセス中にラベルのテキストを変更するコードにアクセスしても、変更は表示されません。

 reloading data
 generating cell contents
 generating cell contents
 setting map distance: 25
 generating cell contents
 setting max customers: 250
 generating cell contents
 setting max products: 150

繰り返しますが、最後の行が正しく更新されるため、途方に暮れています。しかし、他の誰もそうしません。

ありがとう

4

3 に答える 3

0

2 回目では、" " が呼び出されていないことがわかります。つまりadded new label to cell、古い tableViewCell を再利用しています。

_currentSettingセルを再利用する場合は設定せず、新しいセルを作成する場合にのみ設定することに注意してください。その_currentSettingため、作成された最後の新しいセルに設定されます。ほとんどの場合、テーブルの最後のセルです。

正しいラベルに設定する必要があり_currentSettingます (おそらく、viewWithTag: または同様のものを使用して)。

(e:f;b)

于 2013-02-04T15:55:48.897 に答える
0

tableView をリロードすると、セルは既に存在し、tableView からデキューされるため、条件if (cell == nil)は false を返し、セル作成コードは実行されません。

そのセル作成コードでは、値を割り当ててから_currentSetting、その値が正しいと仮定してコードを続行します。ただし、セル作成コードが実行されていない場合、その値は最後に作成されたセルを指すため、更新されません。

これを修正するには: _currentSetting をローカル変数にし、コードを次のように変更します。

(実際にはローカル変数にする必要はありませんが、このメソッドを終了した後に作成した最後のラベルへの参照が実際には必要ないため、より適切です)

UILabel *_currentSetting = nil;
if (cell == nil) {
    _currentSetting = ...
    _currentSetting.tag = 123;
}
else
    _currentSetting = [cell.contentView viewWithTag:123];

...
于 2013-02-04T15:55:55.703 に答える
0

ここでの問題は、2 回目 (ビューをリロードするとき) _currentSetting に有効なメモリがないことです。そのため、カスタム セルを実装してジョブを実行することをお勧めします。

これを優れたガイドとして参照することをお勧めします

于 2013-02-04T16:14:42.207 に答える