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,295评论 4 61
  • 早上出门时天刚亮,从那条熟悉的小路穿过,马上快到地铁站时空气里忽然迷漫开一股烟草的气味。我四下里看看,除了我...
    浑水摸鱼儿阅读 268评论 0 1
  • /*! https://github.com/lzxb/flex.css */ [flex], [flex] > ...
    美滋滋213阅读 3,171评论 1 0
  • 本文是小魔分享给那些同样喜欢LOL的童鞋! 没想到LOL已经陪伴着我们走过了七个年头,这七个年头,小魔在看LOL视...
    Python小魔阅读 1,812评论 8 15
  • 今天有姐妹说到她的一个名牌大学研究生毕业的弟妹最近签约了一家自媒体当全职模特。虽然她完全不知道这个“自媒体”是什么...
    茉莉大大阅读 202评论 0 0