Swift - UITableViewController

文件圖例.png

NewsTableViewController.swift代碼如下:

import UIKit

class NewsTableViewController: UITableViewController {

    let newsReuseIdentifier = "newsCell"
    //定義數組存儲數據模型news對象
    var newsArray:[News] = Array()

    override func viewDidLoad() {
        super.viewDidLoad()
        //調用制造數據的方法(放在最前面)
        self.creatData()

        self.title = "新聞"
        self.navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.9633580072, green: 0.8750048552, blue: 0.9141232886, alpha: 1)
        self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.systemFont(ofSize:30.0),NSForegroundColorAttributeName:#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)]

        //注冊newsCell
        self.tableView.register(NewsCell.self, forCellReuseIdentifier: newsReuseIdentifier)
    }
    //制造數據
    func creatData(){

        for i in 0...10{

            let new = News(newsPicName: "1", newsTitle: "巴西球隊飛機墜毀\(i)", newsContent: "據外媒報道,哥倫比亞民航局表示,調查人員目前已經找到了", newsFollowUp: "27萬人跟帖")
            //添加到數組
            newsArray.append(new)
        }
        //print(newsArray)
    }

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

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return newsArray.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: newsReuseIdentifier, for: indexPath) as! NewsCell

        //取出數組中對應位置的news對象
        let news = newsArray[indexPath.row]
        cell.newsPic.image = UIImage(named:news.newsPicName)
        cell.newsTitleLabel.text = news.newsTitle
        cell.newsContentLabel.text = news.newsContent
        cell.followUpLabel.text = news.newsFollowUp

        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 105
    }

    /*
     // Override to support conditional editing of the table view.
     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
     // Return false if you do not want the specified item to be editable.
     return true
     }
     */

    /*
     // Override to support editing the table view.
     override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
     if editingStyle == .delete {
     // Delete the row from the data source
     tableView.deleteRows(at: [indexPath], with: .fade)
     } else if editingStyle == .insert {
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
     }
     }
     */

    /*
     // Override to support rearranging the table view.
     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

     }
     */

    /*
     // Override to support conditional rearranging of the table view.
     override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
     // Return false if you do not want the item to be re-orderable.
     return true
     }
     */

    /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

NewsCell.swift代碼如下:

import UIKit

//屏幕的寬
let KScreenWidth = UIScreen.main.bounds.size.width
//屏幕的高
let KScreenHeight = UIScreen.main.bounds.size.height

class NewsCell: UITableViewCell {

    //新聞圖片
    var newsPic:UIImageView!
    //標題
    var newsTitleLabel:UILabel!
    //新聞內容
    var newsContentLabel:UILabel!
    //跟帖
    var followUpLabel:UILabel!


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //調用布局的方法
        self.setupViews()
    }

    func setupViews(){

        //新聞圖片
        newsPic = UIImageView(frame: CGRect(x: 10, y: 5, width: 95, height: 95))
        newsPic.backgroundColor = #colorLiteral(red: 0.9052841528, green: 0.8348385063, blue: 1, alpha: 1)
        self.contentView.addSubview(newsPic)

        //標題
        newsTitleLabel = UILabel(frame: CGRect(x: 110, y: 5, width: KScreenWidth - 110 - 5, height: 30))
        newsTitleLabel.backgroundColor = #colorLiteral(red: 0.8630481738, green: 0.9358211487, blue: 0.8020608103, alpha: 1)
        newsTitleLabel.font = UIFont.systemFont(ofSize: 20, weight: 0.3)
        self.contentView.addSubview(newsTitleLabel)

        //新聞內容
        newsContentLabel = UILabel(frame: CGRect(x: 110, y: 40, width: KScreenWidth-115, height: 60))
        newsContentLabel.backgroundColor = #colorLiteral(red: 0.9627746059, green: 0.9764705896, blue: 0.7725070563, alpha: 1)
        //能換行
        newsContentLabel.numberOfLines = 2
        newsContentLabel.textColor = UIColor.lightGray
        self.contentView.addSubview(newsContentLabel)

        //跟帖
        followUpLabel = UILabel(frame: CGRect(x: KScreenWidth-120, y: 70, width: 115, height: 30))
        followUpLabel.textAlignment = .center
        followUpLabel.textColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        //邊框的寬度
        followUpLabel.layer.borderWidth = 1
        //邊框的顏色
        followUpLabel.layer.borderColor = UIColor.lightGray.cgColor
        followUpLabel.layer.cornerRadius = 15
        //followUpLabel.backgroundColor = #colorLiteral(red: 0.9605649373, green: 0.7355437001, blue: 0.7516453615, alpha: 1)
        self.contentView.addSubview(followUpLabel)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

News.swift代碼如下:

import UIKit

class News: NSObject {

    var newsPicName:String!
    var newsTitle:String!
    var newsContent:String!
    var newsFollowUp:String!

    init(newsPicName:String,newsTitle:String,newsContent:String,newsFollowUp:String ) {
        self.newsPicName = newsPicName
        self.newsTitle = newsTitle
        self.newsContent = newsContent
        self.newsFollowUp = newsFollowUp
    }

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,241評論 4 61
  • 早上出門時天剛亮,從那條熟悉的小路穿過,馬上快到地鐵站時空氣里忽然迷漫開一股煙草的氣味。我四下里看看,除了我...
    渾水摸魚兒閱讀 258評論 0 1
  • /*! https://github.com/lzxb/flex.css */ [flex], [flex] > ...
    美滋滋213閱讀 3,153評論 1 0
  • 本文是小魔分享給那些同樣喜歡LOL的童鞋! 沒想到LOL已經陪伴著我們走過了七個年頭,這七個年頭,小魔在看LOL視...
    Python小魔閱讀 1,777評論 8 15
  • 今天有姐妹說到她的一個名牌大學研究生畢業的弟妹最近簽約了一家自媒體當全職模特。雖然她完全不知道這個“自媒體”是什么...
    茉莉大大閱讀 196評論 0 0