美文网首页ios专题
我用Swift写的第一个关于显示列表的demo

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

作者: 旧雨伞时 | 来源:发表于2016-08-18 14:54 被阅读0次

今天下午刚开始学, 虽然之前对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的协议, 就必须实现内部的代理方法, 否则报错.

相关文章

网友评论

    本文标题:我用Swift写的第一个关于显示列表的demo

    本文链接:https://www.haomeiwen.com/subject/qdpvsttx.html