swift demo1 tableview

时间:2022-05-06
本文章向大家介绍swift demo1 tableview,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

代码如下:

//
//  ViewController.swift
//  demo1_tableview
//
//  Created by Alice_ss on 2018/2/24.
//  Copyright © 2018年 AC. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{

    //定义一个tableview
    var tableview :UITableView?

    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableview = UITableView.init(frame: self.view.frame, style: UITableViewStyle.plain)
        self.tableview?.delegate = self
        self.tableview?.dataSource = self
        self.view .addSubview(self.tableview!)
        
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableview?.dequeueReusableCell(withIdentifier: "cellID")
        if cell == nil {
            cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "cellID")
        }
        cell?.textLabel?.text = String(indexPath.row)
        return cell!
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: <#T##CGFloat#>))
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

刚开始看swift项目

坐下笔记:

创建一个简单的tablecview项目,就像是上述代码就可以完成了。

遇到的问题:

1.添加代理的时候老是报错。后来经过百度,在下边的方法中 定义变量的时候在变量的后边加上? 报错就消失了。但是在使用的时候需要加上一个!才能进行。

2.其他的跟oc很类似,就不多介绍了。