您的位置:首页 > 移动开发 > Swift

swift创建tableview

2017-08-01 16:35 239 查看
//

//  ScendViewController.swift

//  SwiftTest

//

//  Created by aojinrui on 2017/8/1.

//  Copyright © 2017年 aojinrui. All rights reserved.

//

import UIKit

class ScendViewController:
UIViewController,UITableViewDataSource,UITableViewDelegate {

    

    var cellArr:[String] = []

    

    

    func youbiananniu() {

        print("点击右边按钮");

    }

    

    override
func viewWillAppear(_ animated:
Bool) {

        super.viewWillAppear(animated)

        

        print("viewWillAppear")

        

        

    }

    

    override
func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor =
UIColor.gray

        self.navigationItem.rightBarButtonItem =
UIBarButtonItem.init(title:
"右边", style:
UIBarButtonItemStyle.plain, target:
self, action:#selector(self.youbiananniu))

        self.navigationItem.title =
"第二页"

        

        for i
in 0...40 {

            cellArr.append("第\(i)个单元格")

        }

        

        

        

        let table =
UITableView.init(frame:
CGRect.init(x:
0, y: 64, width:
self.view.frame.size.width,
height:  self.view.frame.size.height),
style: UITableViewStyle.plain)

        table.delegate =
self;

        table.dataSource =
self;

        self.view.addSubview(table)

        

    }

    

    func tableView(_ tableView:
UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {

        return 
50

    }

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

        return
cellArr.count

    }

    func tableView(_ tableView:
UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {

        

        var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier:
"cell")

        

        if cell==nil {

            cell = UITableViewCell.init(style:
UITableViewCellStyle.default, reuseIdentifier:
"cell")

        }

  

        cell?.selectionStyle =
UITableViewCellSelectionStyle.none

        cell?.textLabel?.text =
cellArr[indexPath.row];

        cell?.backgroundColor =
UIColor.red

        

        return cell!

    }

    

    

    func tableView(_ tableView:
UITableView, didDeselectRowAt indexPath:
IndexPath) {

    

        print("点击了\(indexPath.row)");

        

        

    }

    

    override
func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    /*

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: