簡體   English   中英

iOS如何有效地將UIView添加到tableview單元

[英]iOS how to add UIView to tableview cell effectively

我有一張表,其中每個單元格中都有一個從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;
}

如果我正確理解了您的問題,我認為您想要的解決方案是將UITableViewCell子類化,設置子視圖( UIImageViews以及所有這些子視圖),並將它們設置為子類的屬性。 那么你也能

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

然后只設置圖像。 這樣一來,您一定不會將圖像視圖彼此堆疊,並且在單元出隊時會設置正確的圖像。

編輯:

創建一個名為MyTableViewCell的類(或任何您想調用的類,但要確保它是UITableViewCell的子類)。添加@property(強的,非原子的)UIImageView * imageView;

然后在cellForRowAtIndexPath中使用MyTableViewCell而不是UITableViewCell,您可以訪問自定義表格視圖單元格的image屬性,檢查它是否存在,如果不創建(上面的代碼就是這么做的),然后設置圖像。 我會給你代碼,但是我在打電話。 Google將UITableviewCell子類化。 您應該找到大量示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM