我用Swift写的第一个关于显示列表的demo

今天下午刚开始学, 虽然之前对JS比较熟悉, 但是感觉swift只是结合了JS的一部分特征, 所以写起来略费劲. 并且值得注意的是, 局部变量后面不再引用的话会报错. 恩, 给自己以及初学者提个醒.

下面是代码部分:

1> AppDelegate.swift内:

var window: UIWindow?
 
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        window =  UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        let vc = MyViewController()
        window?.rootViewController = vc
        
        return true
    }

2> 自定义遵守tableView协议的viewController内:

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    var tabelView:UITableView?
    var dataSource:[String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        tabelView = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
        
        tabelView?.delegate = self
        tabelView?.dataSource = self
        self.view = tabelView

        //分割线左间隔为0
        if tabelView!.respondsToSelector("setSeparatorInset:"){
            tabelView?.separatorInset = UIEdgeInsetsZero
        }
        
        if tabelView!.respondsToSelector("setLayoutMargins:"){
            tabelView?.layoutMargins = UIEdgeInsetsZero
        }
        self.initData()
        tabelView?.reloadData()
    }

    func initData(){
        for var i = 0; i < 10; ++i{
            dataSource.append("\(i)")
        }
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 90
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellId = "qqq"  //用 let 关键字声明常量,用 var 关键字声明变量
        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellId)//此处的style不能用default, 否则detailLabel不显示
        cell.textLabel?.text = dataSource[indexPath.row];
       
        cell.detailTextLabel?.text = "game\(indexPath.row)"

        cell.imageView?.image = UIImage(named: "img")
        cell.imageView!.layer.cornerRadius = 10
        cell.imageView!.layer.masksToBounds = true

        //cell分割线居左为0
        if cell.respondsToSelector("setSeparatorInset:"){
            cell.separatorInset = UIEdgeInsetsZero
        }
        
        if cell.respondsToSelector("setLayoutMargins:"){
            cell.layoutMargins = UIEdgeInsetsZero
        }
        
        return cell
    }
}

值得注意的是, 既然遵守了tableview的协议, 就必须实现内部的代理方法, 否则报错.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 在最喜欢的季节里,尽情撒欢~
    青青子衿1225阅读 290评论 0 0
  • 日夜,无度,无眠。 思念,等爱回来。
    mydearyanyan阅读 239评论 0 1
  • 躺在沙发上,听着周华健的《朋友》, “朋友一生一起走,那些日子不在有……” 在这优扬的歌声中...
    华华雯雯阅读 386评论 0 0
  • Assume that your respondents will be reading mail on 80-c...
    吴黄龙本人阅读 237评论 0 0