首页 > 技术文章 > swfit-学习笔记(表UITableView的简单使用)

Zsmile 2015-01-04 13:10 原文

/*使用与Object-C基本类似,只做简单地使用,创建表及其设置数据源和代理*/

import UIKit 

 

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var _tableView = UITableView()    //声明全局变量

    override func viewDidLoad() {

        super.viewDidLoad()

        _tableView.frame = CGRectMake(0, 60, 320, 400)

        _tableView.delegate = self

        _tableView.dataSource = self

        _tableView.separatorStyle = UITableViewCellSeparatorStyle.None  //分隔线

        self.view.addSubview(_tableView)

        

    }

    //设置行数

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    {

        return 6;

    }

    //设置每行显示

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

    {

        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

   if !cell.isKindOfClass(UITableViewCell){

        //关于重用还没搞明白,不知道是不是应该这么判断

        }

        cell.accessoryType = UITableViewCellAccessoryType.DetailButton  //详细按钮

        cell.selectionStyle = UITableViewCellSelectionStyle.None    //是否能选中

        cell.textLabel?.text = "叼炸天!"

        return cell

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

推荐阅读