Swift3.oTableView使用

//
//  ViewController.swift
//  swiftNew
//
//  Created by mibo02 on 17/1/4.
//  Copyright ? 2017年 mibo02. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{

    let btnStartTag:Int = 1000//定義btn的tag起始值
    let name_links_tuples:[(String,String)] = [
    ("第一個(gè)","峰峰"),
    ("第二個(gè)","小明"),
    ("第三個(gè)","小王")
    ]
    let table:UITableView = UITableView.init(frame: UIScreen.main.bounds, style: UITableViewStyle.plain)
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //添加到view上
        self.view.addSubview(table)
        //遵守協(xié)議
        table.dataSource = self
        table.delegate = self
      
    }
    //實(shí)現(xiàn)協(xié)議中的方法
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return name_links_tuples.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: nil)
        
        let newtitle:String = name_links_tuples[indexPath.row].0
        
        
        //每一行有一個(gè)下載的按鈕
        let btn:UIButton = UIButton.init(type: UIButtonType.custom)
        btn.frame = CGRect(x:UIScreen.main.bounds.width - 100, y:10, width:80, height:50)
        btn.backgroundColor = UIColor.red
        btn.setTitle("下載", for: UIControlState.normal)
        btn.setTitleColor(UIColor.black, for: UIControlState.normal)
        btn.tag = indexPath.row + btnStartTag
        btn.addTarget(self, action:#selector(clickBtnAction), for:.touchUpInside)
        
        cell.textLabel?.text = newtitle
        cell.contentView.addSubview(btn)
        return cell
        
    }
    //table的cell的高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        return nil
    }
    func clickBtnAction(sender:UIButton) {
        print("....")
        let indexOfBtn:Int = sender.tag  - btnStartTag
        if indexOfBtn >= 0 && indexOfBtn < name_links_tuples.count {
            let str = name_links_tuples[indexOfBtn].1
            let url = NSURL(string:str)
            if url != nil {
                UIApplication.shared.open(url as! URL, options: [:], completionHandler: nil)
            }
            
            
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

//值得注意的一點(diǎn)是,button的點(diǎn)擊事件,無(wú)論帶不帶參數(shù),寫(xiě)的時(shí)候都不需要加 : 直接將方法名寫(xiě)上即可。
//還有一點(diǎn)就是沒(méi)想到frame的寫(xiě)法已經(jīng)變成了這樣。

屏幕快照 2017-01-04 下午2.34.50.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 6,535評(píng)論 0 17
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,373評(píng)論 25 708
  • 2017.02.22 可以練習(xí),每當(dāng)這個(gè)時(shí)候,腦袋就犯困,我這腦袋真是神奇呀,一說(shuō)讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,378評(píng)論 0 1
  • 現(xiàn)在是2016年12月27日晚上八點(diǎn)三十八分,我想寫(xiě)作,就來(lái)寫(xiě)作了。 我可以在接下來(lái)的文字,不用了嗎? 可以的。保...
    梁超文閱讀 359評(píng)論 0 2
  • 專家說(shuō),為什么不能買別墅?請(qǐng)看下圖
    素麻坊閱讀 2,113評(píng)論 1 4