2013-04-01 39 views
8

Tôi gặp sự cố khi hiển thị các ô trong UICollectionView theo cách nhất quán. Màn hình hiển thị ban đầu của các ô là chính xác, tuy nhiên mỗi khi người dùng cuộn qua và sau đó quay trở lại một nhóm ô, màn hình hiển thị không chính xác. Hàng chỉ nên chứa 2 hoặc 1 ô. 2 ô mỗi ô lấy một nửa chiều rộng màn hình, 1 ô lấy toàn bộ chiều rộng.UICollectionView không bố cục nhất quán, UICollectionViewFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [self preferredSizeForIndexPath:indexPath]; 
} 

- (CGSize)preferredSizeForIndexPath:(NSIndexPath *)indexPath { 

    BOOL isLastObjectInSection = NO; 
    NSString *sectionKey = [[arrCollectionData[indexPath.section] allKeys] objectAtIndex:0]; 
    DLog(@"SectionKey: %@", sectionKey); 
    NSArray *arrSection = [arrCollectionData[indexPath.section] objectForKey:sectionKey]; 
    DLog(@"ArrSection: %@", arrSection); 

    if (arrSection[indexPath.row] == arrSection.lastObject) { 
     if(arrSection.count % 2 != 0) { 
      isLastObjectInSection = YES; 
     } 
    } 

    CGSize cellSize = CGSizeZero; 
    if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) { 
     if (isLastObjectInSection == YES) { 
      cellSize = CGSizeMake(IPAD_BADGE_WIDTH_LANDSCAPE_WIDE, IPAD_BADGE_HEIGHT_LANDSCAPE_WIDE); 
     } else { 
      cellSize = CGSizeMake(IPAD_BADGE_WIDTH_LANDSCAPE, IPAD_BADGE_HEIGHT_LANDSCAPE); 
     } 
    } else { 
     if (isLastObjectInSection == YES) { 
      cellSize = CGSizeMake(IPAD_BADGE_WIDTH_WIDE, IPAD_BADGE_HEIGHT_WIDE); 
     } else { 
      cellSize = CGSizeMake(IPAD_BADGE_WIDTH, IPAD_BADGE_HEIGHT); 
     } 
    } 
    DLog(@"CellSize: %@", NSStringFromCGSize(cellSize)); 
    return cellSize; 

} 

Dưới đây là mẫu dữ liệu thu thập.

Printing description of self->arrCollectionData: 
<__NSArrayI 0x94bbc40>(
{ 
    "March 12, 2013" =  (
     "<FMLeafTimelineContainer: 0x94b2430>", 
     "<FMLeafTimelineContainer: 0x94b3670>" 
    ); 
}, 
{ 
    "February 25, 2013" =  (
     "<FMLeafTimelineContainer: 0x94b4500>" 
    ); 
}, 
{ 
    "February 14, 2013" =  (
     "<FMLeafTimelineContainer: 0x94b48f0>", 
     "<FMLeafTimelineContainer: 0x94b3a60>" 
    ); 
}, 
{ 
    "February 12, 2013" =  (
     "<FMLeafTimelineContainer: 0x94b3ce0>", 
     "<FMLeafTimelineContainer: 0x94b2b00>" 
    ); 
}, 
{ 
    "February 4, 2013" =  (
     "<FMCommunityTimelineContainer: 0x94b4e90>", 
     "<FMCommunityTimelineContainer: 0x94b5050>", 
     "<FMCommunityTimelineContainer: 0x94b5f70>" 
    ); 
}, 
{ 
    "January 30, 2013" =  (
     "<FMCommunityTimelineContainer: 0x94b6ad0>", 
     "<FMCommunityTimelineContainer: 0x94b5a90>" 
    ); 
}, 
{ 
    "January 24, 2013" =  (
     "<FMCommunityTimelineContainer: 0x94b5d00>", 
     "<FMCommunityTimelineContainer: 0x94b6d90>" 
    ); 
}, 
{ 
    "January 22, 2013" =  (
     "<FMCommunityTimelineContainer: 0x94b6440>" 
    ); 
}, 
{ 
    "January 21, 2013" =  (
     "<FMCommunityTimelineContainer: 0x94b6260>", 
     "<FMCommunityTimelineContainer: 0x94b62e0>", 
     "<FMCommunityTimelineContainer: 0x94b70c0>", 
     "<FMCommunityTimelineContainer: 0x94b55a0>", 
     "<FMCommunityTimelineContainer: 0x94b82d0>", 
     "<FMCommunityTimelineContainer: 0x94b78b0>" 
    ); 
}, 
{ 
    "December 20, 2012" =  (
     "<FMCommunityTimelineContainer: 0x94b53f0>" 
    ); 
}, 
{ 
    "December 6, 2012" =  (
     "<FMCommunityTimelineContainer: 0x94b7200>" 
    ); 
}, 
{ 
    "December 4, 2012" =  (
     "<FMCommunityTimelineContainer: 0x94b72b0>" 
    ); 
}, 
{ 
    "November 19, 2012" =  (
     "<FMCommunityTimelineContainer: 0x94b7ae0>" 
    ); 
} 
) 

Các bức ảnh dưới đây minh họa cùng một phần của chế độ xem bộ sưu tập.

Initial Display First scroll past Second scroll past

Trả lời

1

Vấn đề là do một subview của phụ huynh có một kích thước khung hình cố định phân công tại instantiation. Overrode setFrame: dành cho UICollectionView subclass để gán giá trị của khung hình subview và mỗi lần một ô bị khử và lời gọi setFrame được thực hiện: để điều chỉnh cho phù hợp.

0

Bạn có thể thử tính toán động chiều rộng mục.

CGFloat fullItemWidth = self.collectionView.bounds.size.width; 
NSInteger itemsPerRow = 2; 
CGFloat itemWidth = (fullItemWidth - (itemsPerRow-1)*self.collectionView.collectionViewLayout.minimumInteritemSpacing)/itemsPerRow; 
if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) { 
    if (isLastObjectInSection == YES) { 
     cellSize = CGSizeMake(fullItemWidth, IPAD_BADGE_HEIGHT_LANDSCAPE_WIDE); 
    } else { 
     cellSize = CGSizeMake(halfItemWidth, IPAD_BADGE_HEIGHT_LANDSCAPE); 
    } 
} else { 
    if (isLastObjectInSection == YES) { 
     cellSize = CGSizeMake(fullItemWidth, IPAD_BADGE_HEIGHT_WIDE); 
    } else { 
     cellSize = CGSizeMake(halfItemWidth, IPAD_BADGE_HEIGHT); 
    } 
} 
Các vấn đề liên quan