IOS UITableView

来源:互联网 发布:mac webstorm sass 编辑:程序博客网 时间:2024/05/20 05:03




import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {        var ctrlnames:[String]?    var tableView:UITableView?        override func loadView() {        super.loadView()    }        override func viewDidLoad() {        super.viewDidLoad()                        //初始化数据,这一次数据,我们放在属性列表文件里        self.ctrlnames =  NSArray(contentsOfFile:            NSBundle.mainBundle().pathForResource("Controls", ofType:"plist")!) as? Array                print(self.ctrlnames)                        //创建表视图        self.tableView = UITableView(frame: self.view.frame, style:UITableViewStyle.Plain)        self.tableView!.delegate = self        self.tableView!.dataSource = self        //创建一个重用的单元格        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")        self.view.addSubview(self.tableView!)                        //创建表头标签        let headerLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, 30))        headerLabel.backgroundColor = UIColor.blackColor()        headerLabel.textColor = UIColor.whiteColor()        headerLabel.numberOfLines = 0        headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping        headerLabel.text = "常见 UIKit 控件"        headerLabel.font = UIFont.italicSystemFontOfSize(20)        self.tableView!.tableHeaderView = headerLabel    }        //在本例中,只有一个分区    func numberOfSectionsInTableView(tableView: UITableView) -> Int {        return 1;    }        //返回表格行数(也就是返回控件数)    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return self.ctrlnames!.count    }        //创建各单元显示内容(创建参数indexPath指定的单元)    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)        -> UITableViewCell    {        //为了提供表格显示性能,已创建完成的单元需重复使用        let identify:String = "SwiftCell"        //同一形式的单元格重复使用,在声明时已注册        let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)            as UITableViewCell        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator        cell.textLabel?.text = self.ctrlnames![indexPath.row]        return cell    }        // UITableViewDelegate 方法,处理列表项的选中事件    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)    {        self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)                let itemString = self.ctrlnames![indexPath.row]                let alertController = UIAlertController(title: "提示!",            message: "你选中了【\(itemString)】", preferredStyle: UIAlertControllerStyle.Alert)        let okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default,handler: nil)        alertController.addAction(okAction)        self.presentViewController(alertController, animated: true, completion: nil)    }        //滑动删除必须实现的方法    func tableView(tableView: UITableView,        commitEditingStyle editingStyle: UITableViewCellEditingStyle,        forRowAtIndexPath indexPath: NSIndexPath) {            print("删除\(indexPath.row)")            let index = indexPath.row            self.ctrlnames?.removeAtIndex(index)            self.tableView?.deleteRowsAtIndexPaths([indexPath],                withRowAnimation: UITableViewRowAnimation.Top)    }        //滑动删除    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath)        -> UITableViewCellEditingStyle {            return UITableViewCellEditingStyle.Delete    }        //修改删除按钮的文字    func tableView(tableView: UITableView,        titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {            return "删"    }        override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()    }}

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0">    <array>        <string>标签 UILabel</string>        <string>文本框  UITextField</string>        <string>按钮 UIButton</string>        <string>开关按钮 UISwitch</string>        <string>分段控件 UISegmentControl</string>        <string>图像 UIImageView</string>        <string>进度条 UIProgressView</string>        <string>滑动条 UISlider</string>        <string>警告框 UIAlertView</string>    </array></plist>


0 0
原创粉丝点击