swift读取plist文件内容用UITableView展示

前端之家收集整理的这篇文章主要介绍了swift读取plist文件内容用UITableView展示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


plist内容如图



viewController.swift

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    
    var cells : NSDictionary? // Global Variable
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let path = NSBundle.mainBundle().bundlePath
        let plistName:NSString = "Property List.plist"
        let finalPath:NSString = (path as NSString).stringByAppendingPathComponent(plistName as String)
        cells = NSDictionary(contentsOfFile:finalPath as String)
    }
    
    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return cells!.count
    }
    
    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
        
        let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomCell
        let myCell: AnyObject = cells!.objectForKey("cell\(indexPath.row)") as! NSDictionary
        
        cell.myTitle?.text = myCell.objectForKey("title") as? String
        cell.mySubtitle?.text = myCell.objectForKey("subtitle") as? String
        cell.myImage?.image = UIImage(named: myCell.objectForKey("image") as! String)
        
        return cell
    }
    
    func tableView(tableView: UITableView,titleForHeaderInSection section:Int) -> String?  {
        return "TuxMania"
    }
    
    func tableView(tableView: UITableView,titleForFooterInSection section:Int) -> String? {
        return "Get all the Tux"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}

customCell.swift
class CustomCell: UITableViewCell {
    
    @IBOutlet weak var myImage: UIImageView!
    @IBOutlet weak var myTitle: UILabel!
    @IBOutlet weak var mySubtitle: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(selected: Bool,animated: Bool) {
        super.setSelected(selected,animated: animated)
    }
    
}

运行结果

猜你在找的Swift相关文章