2016-07-21 20 views
5

Xin chào mọi người. Tôi bắt đầu học lập trình cách đây 1 tháng, vì vậy hãy tốt nếu tôi không giải thích tốt vấn đề của tôi :)Swift: Mở rộng chiều cao UITableViewCell tùy thuộc vào kích thước của UICollectionView bên trong nó

Dự án của tôi bao gồm UITableView chính. Trong mỗi ô của UITableView, tôi có một số UICollectionView (trên cuộn ngang).

Img 1 : Main view

Các rộng của mỗi UICollectionViewCell cũng giống như toàn bộ UITableViewCell. Vấn đề đầu tiên của tôi là khoảng định kích thước chiều cao của UITableViewCell (tùy thuộc vào kích thước của chính số UICollectionView và kích thước của nội dung trên đầu trang).

Img 2 : CollectionView

Việc này phải được thực hiện tự động. Trên thực tế, UICollectionViewCell s sẽ không có cùng chiều cao, vì vậy khi người dùng cuộn UICollectionView theo chiều ngang, UICollectionViewCell mới sẽ xuất hiện (với chiều cao khác) và chiều cao của UITableViewCell sẽ phải thích ứng.

Vấn đề thứ hai tôi có về kích thước UICollectionViewCell, trên thực tế tôi sẽ không biết trước chiều cao của nó sẽ là gì (chiều rộng là giống như UITableView). Tôi nên nhập nội dung từ một ngòi bút.

Bây giờ đây là tập tin ViewController tôi,

các biến:

@IBOutlet weak var tableView: UITableView! 
var storedOffsets = [Int: CGFloat]() 

phần mở rộng UITableView: Tạo tế bào của UITableView và thiết lập đại biểu của UICollectionView bên trong nó

extension IndexVC: UITableViewDelegate, UITableViewDataSource { 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 6 //Temp 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCellWithIdentifier("CellCollectionView") as? CellPost { 
     let post = self.posts[indexPath.row] 
     cell.configureCell(post) 

     return cell 
    } else { 
     return CellPost() 
    } 

} 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    guard let tableViewCell = cell as? CellPost else { return } 

    tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) 
    tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0 
} 

func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    guard let tableViewCell = cell as? CellPost else { return } 

    storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset 
} 

một phần của UICollectionView: Thêm chế độ xem từ một Nib/xib vào ô

extension IndexVC: UICollectionViewDelegate, UICollectionViewDataSource { 
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 9 //Temp 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellInCollectionView", forIndexPath: indexPath) 

    if let textPostView = NSBundle.mainBundle().loadNibNamed("textPostView", owner: self, options: nil).first as? textPostView { 
     textPostView.configurePost(post.descriptionLbl) 
     cell.addSubview(textPostView) 
     textPostView.translatesAutoresizingMaskIntoConstraints = false 
     cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":textPostView])) 
     cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":textPostView])) 

    } 

    return cell 
} 

Hãy kích thước của các tế bào giống như toàn bộ UICollectionView

extension IndexVC: UICollectionViewDelegateFlowLayout { 
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSizeMake(collectionView.frame.width, collectionView.frame.height) 
} 

Và tôi tạo ra phần mở rộng này trong lớp dành riêng cho UITableViewCell (không hữu ích cho vấn đề, nhưng nếu bất kỳ bạn muốn để tái tạo nó)

extension CellPost { 
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) { 

    collectionView.delegate = dataSourceDelegate 
    collectionView.dataSource = dataSourceDelegate 
    collectionView.tag = row 
    collectionView.setContentOffset(collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling. 
    collectionView.reloadData() 
} 

var collectionViewOffset: CGFloat { 
    set { 
     collectionView.contentOffset.x = newValue 
    } 

    get { 
     return collectionView.contentOffset.x 
    } 
} 

Nếu bất cứ ai muốn sử dụng mã này, nó hoạt động tuyệt vời, nhưng tôi phải hardcode chiều cao của UITableView với một tableView(... heightForRowAtIndexPath ...)

Tôi đã thử mọi thứ để làm cho UITableViewCell thích ứng với nội dung bên trong (Tôi đã thử tính kích thước nội dung được gửi bởi Nib Tôi đang đặt vào ô, sau đó gửi đến tableView(... heightForRowAtIndexPath ...) nhưng tôi không thể làm cho nó hoạt động . Tôi cũng đã thử Auto Layouts nhưng có lẽ tôi đang làm sai. Tôi cũng nghĩ rằng vấn đề có thể đến từ phần mà tôi đã nhập khẩu ngòi trong tế bào của tôi.

Tôi cũng không biết cách mở rộng ô sau khi người dùng đã vuốt UICollectionViewCell, có cách nào để tạo sự kiện khi điều đó xảy ra không? Và có thể gọi lại là tableView(... heightForRowAtIndexPath ...)?

+0

hiếm khi sử dụng nó theo cách này, khi bạn quyết định sử dụng UICollectionView, trong trường hợp nhất, không có nhu cầu sử dụng UITableView để chứa nó tôi nghĩ. – childrenOurFuture

+0

@Jean Baptiste, Câu trả lời được chấp nhận có giải quyết được vấn đề của bạn không? Tôi đang làm chính xác giống như mã của bạn nhưng khi tôi sử dụng UITableViewAutomaticDimension xem bộ sưu tập của tôi không ràng buộc ở tất cả. bạn có thể cho tôi một gợi ý được không? –

Trả lời

1

Khi tôi hiểu bạn cần phải tự động điều chỉnh chiều cao ô xem bảng. vì vậy bạn có thể sử dụng autolayout để điều chỉnh chiều cao của ô.

Viết trong viewDidLoad

self.tableView.estimatedRowHeight = 80 
self.tableView.rowHeight = UITableViewAutomaticDimension 

trước khi trở về tế bào trong cellForRowAtIndexPath

self.tableView.setNeedsLayout() 
self.tableView.layoutIfNeeded() 

Bạn có thể tìm thấy liên kết cho autolayout. enter link description here

+1

Tính năng này không hoạt động. Không chắc chắn tại sao nó được chấp nhận. –

0

Để giải quyết vấn đề của tôi, tôi đã tạo một mảng để lưu trữ các chiều cao của mỗi tế bào của UITableView (tên cellHeight).

Tôi cũng tính chiều cao của mỗi ô khi bắt đầu chương trình trong một mảng bao gồm mảng (có tên là cellHeightForPost). Mảng chính đại diện cho tất cả các TableView cells, mỗi mảng bên trong nó đại diện cho chiều cao của mỗi tế bào của 'collectionView'

Để cập nhật xem bảng mỗi lần tôi thay đổi collectionView bên trong nó, tôi sử dụng fonction collectionView(... willDiplayCell...):

self.tableView.beginUpdates() 
cellHeight[collectionView.tag] = cellHeightForPost[collectionView.tag][indexPath.row] 
self.tableView.setNeedsLayout() 
self.tableView.layoutIfNeeded() 
self.tableView.endUpdates() 

My kích thước UITableView được định nghĩa trong tableView(... heightForRowAtIndexPath)tableView(... estimatedHeightForRowAtIndexPath):

return cellHeight[indexPath.row] 

Để đặt kích thước của collectionViewCells:

return CGSize(width: collectionView.frame.width, height: cellHeightForPost[collectionView.tag][indexPath.row]) 
Các vấn đề liên quan