2015-02-03 2 views
0

전화 번호 블록을 표시하는 UICollectionViewController가 있습니다 (이미지 참조). 보기가로드 될 때 그들은 모두 스크롤을 시작하거나, 회전을 변경하거나, 데이터가 소싱되는 (변경 가능한) 배열을 변경하는 검색 함수를 실행할 때 모두 잘 나타납니다. 잘못된 형식의 레이블을 참조하십시오. 나는 그것이 iOS 시뮬레이터일지도 모른다고 생각했지만, UICollectionViewCells의 위치에 문제가있는 것처럼 보였다.iOS, UICollectionViewCell 형식이 잘못된 UILabel

enter image description here

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"cell"; 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    cell.backgroundColor = [UIColor grayColor]; 
    cell.layer.cornerRadius = 5; 
    [cell setClipsToBounds: YES]; 

    CGRect cellBound = CGRectMake(25, 12.5, 150, 12.5); // x, y, w, h 
    UILabel *title = [[UILabel alloc] initWithFrame:cellBound]; 

    NSString *number = [[searchNumbers objectAtIndex:indexPath.row] valueForKey:@"number"]; 
    number = [number stringByReplacingOccurrencesOfString:@"+44" withString: @"0"]; 
    title.text = number; 

    [cell addSubview:title]; 
    return cell; 
} 

당신이 (또 다시 cellForItemAtIndexPath이기 때문에 휴대에 더 많은 UILabel 파단으로 추가되기 때문에 이런 일이 생각 난 UICollectionViewFlowLayout

답변

1

@Woodstock이 언급했듯이 이는 셀에 UILabel 개체를 "과도하게 추가하기"때문입니다. 당신이보기 설정하고 추가 할, 기본적으로

// A UICollectionViewCell subclass 
// Make sure to pick the correct "init" function for your use case 
- (instancetype)init... { 
    self = [super init...]; 
    if (self != nil) { 
     [self setupCell]; 
    } 
    return self; 
} 
- (void)setupCell { 
    self.backgroundColor = [UIColor grayColor]; 
    self.clipsToBounds  = YES; 
    self.layer.cornerRadius = 5; 
    CGRect cellBound  = CGRectMake(25, 12.5, 150, 12.5); // x, y, w, h 
    // Assumes you've set up a UILabel property 
    self.titleLabel   = [[UILabel alloc] initWithFrame:cellBound]; 
    [cell addSubview:self.titleLabel]; 
} 
- (void)configureWithNumber:(NSString *)number { 
    number     = [number stringByReplacingOccurrencesOfString:@"+44" withString: @"0"]; 
    self.titleLabel.text = number; 
} 

// In your UICollectionViewDataSource/Delegate implementation 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *identifier = @"cell"; 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
    NSString *number   = [[searchNumbers objectAtIndex:indexPath.row] valueForKey:@"number"]; 
    [cell configureWithNumber:number]; 
    return cell; 
} 

: 오히려 여전히 -collectionView:cellForRowAtIndexPath:의 셀에 UILabel을 추가 자신의 솔루션보다

는 더 나은 MVC 솔루션이있다 초기에 셀을 설정합니다. 그런 다음 데이터 값/객체를 전달하고 을 구성하고 셀을 구성해야합니다. 다른 컨트롤 (2 개의 라벨 대 1 개의 라벨 등)이 필요한 셀이있는 경우 여러 개의 하위 클래스를 만듭니다. 이렇게하면 코드를보다 깨끗하게 정리하고 재사용 할 수 있도록 클래스를 캡슐화 할 수 있습니다.

+0

당신은 완전히 맞습니다 :) 내 대답은 잘못된 것을 올바른 방법으로 설명하는 것이 아니라 upvoted를 설명합니다! – Woodstock

+0

이것을 달성하는 아주 좋은 방법 :) –

1

를 사용하고 있음을 주목해야한다 라는). 셀에 아직이 없으면 수표를 추가하고 레이블 하위 뷰만 추가해야합니다. 대기열에서 제외 된 셀은 이미 재사용되고있는 경우 레이블 하위 뷰가 있습니다.이 레이블이 이미 존재하면 데이터 소스의 텍스트를 설정하기 만하면됩니다.

의사 코드 :

for subview in subviews { 
    if subview.isKindOfClass(UILabel) { 
     // assign the new text label. 
    } 
    else 
    { 
     // create and add the UILabel subView. 
    } 

} 

dequeueReusableCellWithReuseIdentifier는 당신이 이전에 사용 된 세포를 제공 또는 당신이 보았 듯이 처음에 당신에게 새로운 하나를 줄 수 있습니다로 만들 수있는 쉬운 실수이다. 그래서 앱을 시작하면 올바르게 작동하지만 스크롤 할 때 더러워집니다.