swift之自定义表格控件(UITableView)

swift之自定义表格控件(UITableView),第1张

概述1,效果图 2、控件 storyboard上的控件就2个:UIButton。 3、为按钮添加点击事件 通过辅助编辑器为这2个按钮添加按钮单击事件:分别为 generalBtnClick 和   groupBtnClick 4、完整代码: import UIKitenum UIControlType{ case Basic case Advanced}class ViewC 1,效果图


2、控件

storyboard上的控件就2个:UIbutton。

3、为按钮添加点击事件

通过辅助编辑器为这2个按钮添加按钮单击事件:分别为 generalBtnClick 和 groupBtnClick

4、完整代码:
import UIKitenum UIControlType{    case Basic    case Advanced}class VIEwController: UIVIEwController,UItableVIEwDelegate,UItableVIEwDataSource{        var tableVIEw:UItableVIEw?        var ctrlnames:[String]? = ["按钮","文本框","标签"];        var allnames:Dictionary<Int,[String]>?        var adheaders:[String]?        var ctype:UIControlType!        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,terminator: "")                //初始化数据,这一次数据,我们放在属性列表文件里        self.allnames =  [ 0:[String](self.ctrlnames!),1:[String]([            "日期选择器","网页选择器","工具条","表格视图"])        ];                //        print(self.allnames,terminator: "")                self.adheaders = [            "常见UIKit控件","高级UIKit控件"        ]    }        @IBAction func generalBtnClicked(sender: UIbutton) {        self.ctype = UIControlType.Basic                        //创建表视图        self.tableVIEw = UItableVIEw(frame:CGRectMake(0,100,self.vIEw.frame.size.wIDth,self.vIEw.frame.size.height - 100),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,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    }        @IBAction func groupBtnClicked(sender: UIbutton) {        self.ctype = UIControlType.Advanced                //创建表视图                        self.tableVIEw = UItableVIEw(frame:CGRectMake(0,style:UItableVIEwStyle.Grouped)        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,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 self.ctype == UIControlType.Basic ? 1:2;    }        //返回表格行数(也就是返回控件数)    func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {        let data = self.allnames?[section]        return data!.count    }            // UItableVIEwDataSource协议中的方法,该方法的返回值决定指定分区的头部    func tableVIEw(tableVIEw:UItableVIEw,TitleForheaderInSection        section:Int)->String?    {        var headers =  self.adheaders!;        return headers[section];    }    // UItableVIEwDataSource协议中的方法,该方法的返回值决定指定分区的尾部    func tableVIEw(tableVIEw:UItableVIEw,TitleForFooterInSection        section:Int)->String?    {        let data = self.allnames?[section]        return "有\(data!.count)个控件"    }            //创建各单元显示内容(创建参数indexPath指定的单元)    func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell    {        let IDentify:String = "SwiftCell";                /// 同一形式的单元格重复使用。        let secno = indexPath.section;        var data = self.allnames?[secno];        if (0 == secno)        {            let cell = tableVIEw.dequeueReusableCellWithIDentifIEr(IDentify,forIndexPath: indexPath);            cell.accessoryType = UItableVIEwCellAccessoryType.disclosureIndicator;                        cell.imageVIEw?.image = UIImage(named: "1");            cell.textLabel?.text = data![indexPath.row];                    return cell;        }                else        {            let adcell = UItableVIEwCell(style: .SubTitle,reuseIDentifIEr: "SwiftCell");            adcell.textLabel?.text = data![indexPath.row];            adcell.detailTextLabel?.text = "这是\(data![indexPath.row])的说明";                        return adcell;        }    }        // UItableVIEwDelegate 方法,处理列表项的选中事件    func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath)    {        self.tableVIEw!.deselectRowAtIndexPath(indexPath,animated: true)                let itemString = self.ctrlnames![indexPath.row]                let  alert = UIAlertController(Title: "提示",message: "你选择了:\(itemString)",preferredStyle: UIAlertControllerStyle.Alert);        let sureAction = UIAlertAction(Title: "确定",style: UIAlertActionStyle.Default,handler: {(action)->VoID in});        alert.addAction(sureAction);                presentVIEwController(alert,animated:true,completion:nil);            }        overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()                // dispose of any resources that can be recreated.    }}
总结

以上是内存溢出为你收集整理的swift之自定义表格控件(UITableView)全部内容,希望文章能够帮你解决swift之自定义表格控件(UITableView)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1078963.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存