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控件详解
原文链接:https://www.f2er.com/swift/324433.html