美文网首页Swift专题
Swift基础~UITableView

Swift基础~UITableView

作者: 记忆的北极 | 来源:发表于2018-04-10 12:27 被阅读22次

    从2014年开始,Swift已经发布有4年多了,从刚开始的1.0版本慢慢迭代到了目前的4.0,在网上看了看,使用Swift开发的开发人员也不少了.但是关于Swift4.0的一些教程却是凤毛麟角,少的可怜.所以,我觉得慢慢记录一下自己的Swift开发经验是很有必要的.慢慢学习,慢慢进步.做iOS开发也有一段时间了,本来就应该建立自己的知识体系,让自己零碎的知识统一和组织一下是很重要的.

    现在开始记录一下,关于Swift的学习,我觉得不需要从什么头开始一点一点的看Switf基础语法啃骨头,而是在项目或者自己的一些demo中去学习这些东西,因为这样的效率是非常高的,可以为自己节约非常多的时间,而且当你复习重温的时候也很方便.

    好了,现在开始粘上我的代码,用Swift创建简单的表,UITableView

    import UIKit
    
    class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
     
        let name_lins_lists = [("swift-01","www.baidu.com"),("swift-02","www.baidu.com"),("swift-03","www.baidu.com"),("swift-04","www.baidu.com"),("swift-05","www.baidu.com")]
        let buttonTag:Int = 1000
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            //let rect = self.view.frame
            let rect1 = CGRect(x:0, y:0, width:100, height:100)
            let rect2 = CGRect(x:10, y:10, width:self.view.frame.size.width, height:self.view.frame.size.height)
            let table:UITableView = UITableView(frame:rect1)
            table.delegate = self;
            table.dataSource = self;
            self.view.addSubview(table)
            
        }
    
        //MARK:-
        //MARK: delegate
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return name_lins_lists.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = (tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath)) as UITableViewCell
            cell.detailTextLabel?.text = name_lins_lists[indexPath.row].0
            
            let button:UIButton = UIButton(frame:CGRect(x:150, y:10, width:100, height:30))
            button.setTitle("下载", for: UIControlState.normal)
            button.setTitleColor(UIColor.red, for: UIControlState.normal)
            button.tag = buttonTag + indexPath.row
            button.addTarget(self, action: #selector(ViewController.buttonMethod(sender:)), for: UIControlEvents.touchUpInside)
            cell.addSubview(button)
            return cell
        }
        
        @objc func buttonMethod(sender:UIButton) {
            print("button click ",(sender.tag))
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    
    

    相关文章

      网友评论

        本文标题:Swift基础~UITableView

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