2012-09-26 3 views
0

XMLParser 피드에서 공급되는 UIView가있는 각 셀이있는 테이블이 있습니다. 내가 그것을 어떻게 남겨두면, 내가보기를 새로 고침 할 때마다 그들은 풀어 지거나 제거되지 않기 때문에 서로에 기반을 둡니다. 이는 완전히 이해가됩니다. 어떻게하면 문제를 피하기 위해 내 코드를 작성하고 새로 고침 할 때마다 뷰를 다시 작성하지 않아도됩니다. 나는 그것의 논리와 실제 구문에 갇혀있다. 감사.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를 을 * 이미지 뷰;

그런 다음 cellForRowAtIndexPath에서 UITableViewCell 대신 MyTableViewCell을 사용하고 사용자 정의 테이블보기 셀의 이미지 속성에 액세스하여 존재하지 않는지 확인한 다음 (위 코드에서와 같이) 이미지를 설정합니다 . 나는 당신에게 코드를 줄 것이다. 그러나 나는 나의 전화 위에있다. Google은 UITableviewCell을 서브 클래 싱합니다. 몇 가지 예를 찾아야합니다.

+0

입니다. imageView 하위 클래스의 이름을 보시겠습니까? Sory 저는 iOS에 익숙하지 않습니다. –

+0

좋아, 훨씬 더 의미가 있습니다. 고마워. –

관련 문제