使用Xib实现团购Demo

今天用Xib实现了一个简单的团购demo,废话不多说,直接上效果图:

Paste_Image.png
代码如下:

plist文件

Paste_Image.png

1.在自定义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.png

3.在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
    } 
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,620评论 25 708
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,586评论 7 249
  • filter 使用异步操作对集合中的元素进行筛选。需要注意的是,iterator的callback只有一个参数,只...
    LuciferTM阅读 661评论 0 0
  • 工作笔记(不加附注了) 由于今天忘了带复读机,所以晚间没有联系,今天共计练习时间1.40。即将寻找材料,复习。 晚...
    忽尔今至阅读 217评论 0 0