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
}
}