Swift - UITableViewController

文件圖例.png

NewsTableViewController.swift代碼如下:

import UIKit

class NewsTableViewController: UITableViewController {

    let newsReuseIdentifier = "newsCell"
    //定義數(shù)組存儲(chǔ)數(shù)據(jù)模型news對(duì)象
    var newsArray:[News] = Array()

    override func viewDidLoad() {
        super.viewDidLoad()
        //調(diào)用制造數(shù)據(jù)的方法(放在最前面)
        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)]

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

        for i in 0...10{

            let new = News(newsPicName: "1", newsTitle: "巴西球隊(duì)飛機(jī)墜毀\(i)", newsContent: "據(jù)外媒報(bào)道,哥倫比亞民航局表示,調(diào)查人員目前已經(jīng)找到了", newsFollowUp: "27萬(wàn)人跟帖")
            //添加到數(shù)組
            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

        //取出數(shù)組中對(duì)應(yīng)位置的news對(duì)象
        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!
    //標(biāo)題
    var newsTitleLabel:UILabel!
    //新聞內(nèi)容
    var newsContentLabel:UILabel!
    //跟帖
    var followUpLabel:UILabel!


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //調(diào)用布局的方法
        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)

        //標(biāo)題
        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)

        //新聞內(nèi)容
        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
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容