2012-09-26 45 views
0

我有一個表格,每個單元格都有一個UIView,它是從XMLParser提要饋入的。如果我放棄它,每當我刷新視圖時,都會因爲它們沒有被釋放/移除而互相建立,這是完全合理的。我將如何去編寫我的代碼以避免這個問題,而不必在每次刷新時都重建視圖。我陷入了它的邏輯和實際的語法。謝謝。iOS如何將UIView有效地添加到tableview單元

代碼:

- (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; 
} 

回答

1

如果我正確理解你的問題,我想你想解決的辦法是繼承UITableViewCell,請設置您的子視圖(UIImageViews和所有),並讓他們在你的子類的屬性。那麼你可以

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

然後只需設置圖像。這樣你就不會將圖像視圖堆疊在一起,並且在單元出列時正確的圖像將被設置。

編輯:

建立一個叫做類MyTableViewCell(或任何你想調用它,但要確保它的UITableViewCell的子類) 添加@property(強,非原子)的UIImageView * ImageView的;

然後在你的cellForRowAtIndexPath中使用MyTableViewCell代替UITableViewCell,你可以訪問自定義表格視圖單元格的圖像屬性,檢查它是否存在,如果沒有創建它(這是上面的代碼所做的),然後設置圖像。我會給你代碼,但我在我的手機上。 Google子類化UITableviewCell。你應該找到很多例子。

+0

是imageView的子類的名稱? Sory我是iOS新手。 –

+0

好吧,這使得更多的意義。非常感謝。 –

相關問題