0

I have a table going where each cell has a UIView in it that is fed from an XMLParser feed. If I leave it how it is, each time I refresh the views build upon each other because they are not getting released/removed, which makes total sense. How would I go about writing my code to avoid that problem, and not having to rebuild the view's each time I refresh. I am stuck in the logic of it and also the actual syntax. Thanks.

Code:

- (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 中使用 MyTableViewCell 而不是 UITableViewCell ,你可以访问你的自定义表格视图单元格的图像属性,检查它是否存在,如果不创建它(这是上面的代码所做的)然后设置图像。我会给你代码,但我在我的手机上。谷歌子类化 UITableviewCell。你应该找到大量的例子。

于 2012-09-26T01:52:18.423 回答