[置顶] Swift基础之UITableView(之前写的知识点都是最新的2.2版本样式,欢迎大家参考,可以相互交流)

//这里只是列举了经常使用的UITableView的属性和方法,其他的都可以类似使用,注意用法即可
    //设置全局变量UITableView
    var myTableView = UITableView();
    //设置数据源数组
    var dataArray = NSArray();
    
    //系统生成的viewDidLoad()方法
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.title = "UITableView";
        self.view.backgroundColor = UIColor.lightGrayColor();
        
        //数据源添加数据
        dataArray = ["头像","昵称","账号","哈哈","呵呵"];
        
        //UITableView设置
        myTableView = UITableView.init(frame: self.view.bounds, style: .Plain);
        //myTableView.rowHeight = 50;
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.tableHeaderView = UIView.init();
        myTableView.tableFooterView = UIView.init();
        //研究了半天总算解决了分割线有空白的问题,调用系统的方法形式跟自定义形式创建的方法不一样,可以参考之前写的UIButton和UIImageView的blog
        if(myTableView.respondsToSelector(Selector("setLayoutMargins:")))
        {
            myTableView.layoutMargins = UIEdgeInsetsZero;
        }
        if myTableView.respondsToSelector(Selector("setSeparatorInset:"))
        {
            myTableView.separatorInset = UIEdgeInsetsZero;
        }
        self.view.addSubview(myTableView);
        
    }
    //Swift中的标记形式: " //MARK: "
    //MARK: --- UITableViewDelegate和UITableViewDataSource
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count;
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("cell");
        if (cell==nil)
        {
            cell = UITableViewCell.init(style: .Value1, reuseIdentifier: "cell");
        }
        if(cell!.respondsToSelector(Selector("setLayoutMargins:")))
        {
            cell!.layoutMargins = UIEdgeInsetsZero;
        }
        if(cell!.respondsToSelector(Selector("setSeparatorInset:")))
        {
            cell!.separatorInset = UIEdgeInsetsZero;
        }
        cell?.textLabel?.text = dataArray[indexPath.row] as? String;
        cell?.imageView?.image = UIImage(named: "login_pwd.png")
        if (indexPath.row == 0)
        {
            cell?.imageView?.image = UIImage(named: "per_info_girl.png");
        }
        //右侧箭头形式
        cell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator;
        
        return cell!;
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("点击了第\(indexPath.row)行....");
        
        let oneVC = OneViewController();
        oneVC.myString = "点击了\(indexPath.row)行";
        self.navigationController?.pushViewController(oneVC, animated: true);
        
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if (indexPath.row == 0)
        {
            return 80;
        }
        else
        {
            return 50;
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
        print("内存警告");
    }

你可能感兴趣的:(Class,swift,全局变量,UITableView)