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控件详解

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...