2014-07-13 2 views
0

내가 한 일은 편리한 label.SizeToFit()을 사용하여 크기를 조정하는 레이블을 설정하는 것이지만 이제는 크기를 적절하게 조정하도록 셀을 설정해야합니다. ./작은 CGSize의 나를 매우 얇은 제공하지만UICollectionView의 셀 크기를 조정하는 방법

messages[indexPath.row].sizeWithAttributes(nil) 

:

나는에 셀 크기를 설정 FlowLayoutDelegate에서 sizeForIndexPath 방법을 사용했다.

import UIKit 

let reuseIdentifier = "Cell" 

class PhotoCollectionViewController: UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 
    var messages = NSString[]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     messages = ["Hello, How are you?", "Ldsfsd sdf dsfs s fs fs", "fsdfsdfsdfs fsdfsdfsdf sfs fs", "yoyo yoy oyo", "sdfd sfds fssfs"] 

     self.collectionView.registerClass(TextCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 
     self.collectionView.dataSource = self 
     self.collectionView.delegate = self 
     self.collectionView.collectionViewLayout = PhotoCollectionViewFlowLayout() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    // #pragma mark UICollectionViewDataSource 

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int { 
     return 1 
    } 


    override func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int { 
     //#warning Incomplete method implementation -- Return the number of items in the section 
     return messages.count 
    } 

    override func collectionView(collectionView: UICollectionView?, cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? { 
     let cell = collectionView?.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as TextCollectionViewCell 
     cell.backgroundColor = UIColor.lightGrayColor() 
     cell.newLabel.text = messages[indexPath!.row] 
     cell.newLabel.sizeToFit() 

     return cell 
    } 

    func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize { 
     //println(messages[indexPath.row].sizeWithAttributes(nil)) 
     return messages[indexPath.row].sizeWithAttributes(nil) 
    } 
} 

편집 : 나는 (AN 최적화되지 않은 방식으로) 그렇다고, 일을 가지고있다 : 그것은 맞지 않는 가 더 긴 경우 :

func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize { 
    let cell = TextCollectionViewCell(frame: CGRect(x: 0, y: 8, width: 300, height: 100)) 
    cell.newLabel.text = messages[indexPath.item] 
    cell.newLabel.sizeToFit() 

    println(cell.newLabel.intrinsicContentSize()) 
    //var width = UIWindow. 
    return CGSizeMake(cell.newLabel.intrinsicContentSize().width+10, cell.newLabel.intrinsicContentSize().height+20) 
    //return cell.newLabel.intrinsicContentSize() 
} 

이제 텍스트 그러나 대부분의 경우에 적합 너비가 화면 크기보다 작 으면 분명히 새 줄이 자동으로 시작되지 않습니다. 또한 화면 중간에 표시됩니다. 레이아웃을 변경할 수있는 위치 또는 다른 위치에 의존하는지 여부는 알 수 없습니다.

+0

셀의 콘텐츠 포깅 우선 순위를 설정 했습니까? 그렇지 않다면 크기 속성을 열고 1000으로 설정하십시오. – NRitH

+0

그 이유는 인터페이스 빌더가 Xcode 6 http의 셀 레이블에 문제가있어서 UICollectionViewCell을 서브 클래스하고 프로그래밍 방식으로 레이블을 추가해야했기 때문입니다. : //stackoverflow.com/questions/24492275/label-outlet-on-custom-uicollectionviewcell-in-swift-causing-optional-none-crash – user3784622

+0

'.sizeWithAttributes()'는 실제 레이블 텍스트의 속성으로 호출해야합니다 . – duci9y

답변

1

UICollectionViewFlowLayout과 함께 자체 크기 조정 셀을 사용하려면 estimatedItemSize을 CGSize가 아닌 크기로 설정해야합니다.

관련 문제