swift读取plist文件内容用UITableView展示
发布时间:2020-12-14 06:54:21 所属栏目:百科 来源:网络整理
导读:plist内容如图 viewController.swift class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { var cells : NSDictionary? // Global Variable override func viewDidLoad() { super.viewDidLoad() let path = NSBundle.mainBu
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) } } 运行结果
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |