2017-06-25 13 views
0

如何使我的表視圖看起來更像這樣,而不僅僅是標準的水平單元格。我希望它看起來像下面的示例圖片,我需要做什麼?我的tableView代碼也在下面。如何使TableView單元格在水平和垂直方向上具有不同的大小?

enter image description here enter image description here

import UIKit 
import Firebase 
import FirebaseDatabase 
import SDWebImage 

struct postStruct { 
    let title : String! 
    let author : String! 
    let date : String! 
    let article : String! 
    let downloadURL : String! 

} 

class NewsViewController: UITableViewController { 
    var posts = [postStruct]() 
    override func viewDidLoad() { 
    super.viewDidLoad() 


    let ref = Database.database().reference().child("Posts") 

    ref.observeSingleEvent(of: .value, with: { snapshot in 
     print(snapshot.childrenCount) 
     for rest in snapshot.children.allObjects as! [DataSnapshot] { 
      guard let value = rest.value as? Dictionary<String,Any> else { continue } 
      guard let title = value["Title"] as? String else { continue } 
      guard let downloadURL = value["Download URL"] as? String else { continue } 
      guard let author = value["Author"] as? String else { continue } 
      guard let date = value["Date"] as? String else { continue } 
      guard let article = value["Article"] as? String else { continue } 

      let post = postStruct(title: title, author: author, date: date, article: article, downloadURL: downloadURL) 
      self.posts.append(post) 
     } 
     self.posts = self.posts.reversed(); self.tableView.reloadData() 
    }) 
} 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return posts.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
     let label1 = cell?.viewWithTag(1) as! UILabel 
     label1.text = posts[indexPath.row].title 
     let imageView = cell?.viewWithTag(2) as! UIImageView 
     let post = self.posts[indexPath.row]; 
     imageView.sd_setImage(with: URL(string: post.downloadURL), placeholderImage: UIImage(named: "placeholder")) 
     return cell! 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "detail" { 
      if let indexPath = tableView.indexPathForSelectedRow { 
       let destVC = segue.destination as! DetailNewsViewController 
       destVC.titleText = posts[indexPath.row].title 
       destVC.dateText = posts[indexPath.row].date 
       destVC.authorText = posts[indexPath.row].author 
       destVC.bodyText = posts[indexPath.row].article 
       destVC.headerPhoto = posts[indexPath.row].downloadURL 
      } 
     } 
    } 
} 
+2

使用'CollectionView'與'flowLayout'你會得到這種類型的上市,,,, – Dhiru

回答

0

也許你可以使用不同的細胞在你的故事板不同的標識符。根據需要設計每個單元格並使用單元格rowAtIndexPath返回所需的單元格。

相關問題