今天用Xib实现了一个简单的团购demo,废话不多说,直接上效果图:
Paste_Image.png代码如下:
plist文件
Paste_Image.png1.在自定义ITCASTgroupBuyCell中:
import UIKit
class ITCASTgroupBuyCell: UITableViewCell {
@IBOutlet weak var foodLabel: UILabel!
@IBOutlet weak var foodImageView: UIImageView!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var buyCountLabel: UILabel!
// 加载xib
override func awakeFromNib() {
super.awakeFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
2.对应的ITCASTgroupBuyCell.xib,这里给大家截个图
Paste_Image.png3.在ViewControllerl中:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let reuseIdentifier = "ITCASTgroupBuyCell"
private var tgs = NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("tgs", ofType: "plist")!)
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
//去除单元格分隔线
self.tableView!.separatorStyle = .None
self.tableView.registerNib(UINib(nibName: "ITCASTgroupBuyCell", bundle: nil), forCellReuseIdentifier: reuseIdentifier)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tgs!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! ITCASTgroupBuyCell
let data:NSDictionary = tgs![indexPath.row] as! NSDictionary
print(data)
cell.foodLabel.text = data["title"] as? String
cell.foodImageView.image = UIImage(named: (data["icon"] as? String)!)
cell.priceLabel.text = "¥" + (data["price"] as? String)!
cell.buyCountLabel.text = (data["buyCount"] as? String)! + "人已购买"
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0
}
}
网友评论