0

XMLParserフィードからフィードされるUIViewが各セルに含まれるテーブルがあります。そのままにしておくと、ビューを更新するたびに、ビューが解放/削除されないため、ビューが相互に構築されます。これは完全に理にかなっています。この問題を回避するためにコードを記述し、更新するたびにビューを再構築する必要がないようにするにはどうすればよいでしょうか。私はそれの論理と実際の構文に固執しています。ありがとう。

コード:

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

  static NSString *CellIdentifier = @"Cell";
  JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
  self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture.png"]];

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

    UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 200, 65)];
    [selectionView setBackgroundColor: [UIColor clearColor]];
    cell.selectedBackgroundView = selectionView;
  }

  // Display content in each cell

  cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 300, 65)];
  [cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
   UIViewAutoresizingFlexibleRightMargin |
   UIViewAutoresizingFlexibleWidth];
  [cellSeparator setContentMode:UIViewContentModeTopLeft];
  [cellSeparator setBackgroundColor: [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0]];
  cellSeparator.layer.borderWidth = 1.0;
  cellSeparator.layer.borderColor = [UIColor blackColor].CGColor;
  [cell addSubview:cellSeparator];

  callTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 190, 21)];
  callTypeLabel.text =  [currentCall currentCallType];
  callTypeLabel.backgroundColor = [UIColor clearColor];
  callTypeLabel.font = [UIFont boldSystemFontOfSize:12.0];
  [cellSeparator addSubview:callTypeLabel];

  locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 17 , 190, 15)];
  locationLabel.text = [currentCall location];
  locationLabel.backgroundColor = [UIColor clearColor];
  locationLabel.font = [UIFont systemFontOfSize:10.0];
  [cellSeparator addSubview:locationLabel];

  unitsLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 43, 190, 21)];
  unitsLabel.text = [currentCall units];
  unitsLabel.backgroundColor = [UIColor clearColor];
  unitsLabel.font = [UIFont italicSystemFontOfSize:10.0];
  [cellSeparator addSubview:unitsLabel];

  stationLabel = [[UILabel alloc]initWithFrame:CGRectMake(195 , 25, 75, 20)];
  NSString *station = [@"Station: " stringByAppendingString:currentCall.station];
  stationLabel.text = station;
  stationLabel.backgroundColor = [UIColor clearColor];
  stationLabel.font = [UIFont boldSystemFontOfSize:11.0];
  [cellSeparator addSubview:stationLabel];

  if ([currentCall.county isEqualToString:@"W"]) {
    UIImage  *countyImage = [UIImage imageNamed:@"blue.png"];
    CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
    UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
    countyImageLabel.image = countyImage;
    [cellSeparator addSubview:countyImageLabel];
  } else {
    UIImage  *countyImage = [UIImage imageNamed:@"green.png"];
    CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
    UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
    countyImageLabel.image = countyImage;
    [cellSeparator addSubview:countyImageLabel];
  }

  if ([currentCall.callType isEqualToString:@"F"]) {
    UIImage  *callTypeImage = [UIImage imageNamed:@"red.png"];
    CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
    UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    callTypeImageLabel.image = callTypeImage;
    [cellSeparator addSubview:callTypeImageLabel];
  } else {
    UIImage  *callTypeImage = [UIImage imageNamed:@"yellow.png"];
    CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
    UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
    callTypeImageLabel.image = callTypeImage;
    [cellSeparator addSubview:callTypeImageLabel];
  }

  return cell;
}
4

1 に答える 1

1

私があなたの質問を正しく理解しているなら、あなたが望む解決策は、サブクラス化UITableViewCellし、サブビュー(UIImageViewsおよびそのすべて)を設定し、それらをサブクラスのプロパティにすることだと思います。その後、あなたはすることができます

if (!cell.imageView) {
    cell.imageView = [[UIImageView alloc] initWithFrame:myFrame];
}

次に、画像を設定します。そうすれば、画像ビューを積み重ねないようにすることができ、セルがデキューされたときに正しい画像が設定されます。

編集:

と呼ばれるクラスをMyTableViewCell作成します(または呼び出したいものは何でも、UITableViewCellのサブクラスであることを確認してください)@property(strong、nonatomic)UIImageView*imageView;を追加します。

次に、cellForRowAtIndexPathでUITableViewCellの代わりにMyTableViewCellを使用すると、カスタムテーブルビューセルのimageプロパティにアクセスし、存在するかどうかを確認し、存在しない場合は作成し(上記のコードで実行)、画像を設定します。私はあなたにコードを与えるでしょうが、私は私の電話にいます。Googleのサブクラス化UITableviewCell。たくさんの例が見つかるはずです。

于 2012-09-26T01:52:18.423 に答える