大佬教程收集整理的这篇文章主要介绍了swiftt之表格控件(UITableView)详解,出错,求指南,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1、问题描述
最后的界面上什么都没有,空白的。如下图:
2、敢问:问题出在什么地方?
3、代码如下:
import UIKit enum UIControlType { case Basic; case Advanced; } class ViewController: UIViewController,UITableViewDatasource,UITableViewDelegate{ var tableView:UITableView?; var ctrNames:[String] = ["按钮UIButton","标签UILabel","文本框UITextField"]; var allNames:Dictionary<Int,[String]>?; var adHeader:[String]?; var controlType:UIControlType?; override func loadView() { super.loadView(); } override func viewDidLoad() { super.viewDidLoad() /// 初始化数据,把这次数据放在属性列表文件里 allNames = [0:ctrNames,1:[String]([ "UIDatePicker 日期选择器","UIWebView 网页选择器","UIToolBar 工具条"])]; self.adHeader = ["常见 UIkit 控件","高级UIKIt控件"]; } @IBACtion func plainClicked(sender:UIBarButtonItem) { controlType = UIControlType.basic; /// 创建表视图 self.tableView = UITableView(frame: CGRectMake(0,100,view.frame.size .width,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,view.bounds.size .width,30)); headerLabel.BACkgroundColor = UIColor.blackColor(); headerLabel.textColor = UIColor.whiteColor(); headerLabel.numberOfLines = 0; headerLabel.lineBreakmode = NSLineBreakmode.byCharWrapping; headerLabel.text = "常见UIKit控件"; headerLabel.font = UIFont.italicSystemFontOfSize(20); /// 将label显示到TableView中 self.tableView!.tableHeaderView = headerLabel; } @IBACtion func groupClickd(sender:UIBarButtonItem) { controlType = UIControlType.Advanced; /// 创建视图 self.tableView = UITableView(frame: CGRectMake(0,view.frame.size.width,view.frame.size .height - 100),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.byCharWrapping; headerLabel.text = "高级UIKit控件"; headerLabel.font = UIFont.italicSystemFontOfSize(20); self.tableView!.tableHeaderView = headerLabel; } /// 创建分区 func numberOfSectionsInTableView(table:UITableView)->Int { return controlType == UIControlType.basic ? 1:2; } /// 返回行数 func tableView(tableView:UITableView,numberOfRowsInSection section:int)->Int { let data = allNames![section]; return data!.count; } /// 协议中得方法,该方法的返回值决定指定分区的头部 func tableView(table:UITableView,titleForHeaderInSection section:int)->String? { return adHeader?[section]; } /// 协议中的方法,该方法的返回值决定指定分区的尾部 func tableView(table:UITableView,titleForFooterInSection section:int)->String? { return "有\(allNames![section]!.count)个控件"; } func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell { let identify = "SwiftCell"; let cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath); cell.accessoryType = UITableViewCellAccessoryType.DisclosureInDicator; let secno = indexPath.section; let data = allNames![secno]; cell.textLabel?.text = data![indexPath.row]; return cell; } /// UItableViewDelegate 方法,处理列表项的选中事件 func tableView(tableView: UITableView,didSELEctRowATindexPath indexPath: NSIndexPath) { tableView.deSELEctRowATindexPath(indexPath,animated: truE); let itemString = ctrNames[indexPath.row]; /// 创建提示框 let alertView = UIAlertController(title: "提示",message: "你选中了\(itemString)",preferredStyle: .Alert); /// 向提示框中增加按钮 let alertAction = UIAlertAction(title: "确定",style: UIAlertActionStyle.Default,handler: {(action)->Void in}); alertView.addAction(alertAction); presentViewController(alertView,animated:true,completion:nil); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }4、UITableView控件详解
以上是大佬教程为你收集整理的swiftt之表格控件(UITableView)详解,出错,求指南全部内容,希望文章能够帮你解决swiftt之表格控件(UITableView)详解,出错,求指南所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。