swift demo1 tableview

本文介绍了一个简单的TableView项目实现过程,并分享了在Swift中创建TableView的基本步骤。包括如何定义TableView、设置代理方法、加载单元格等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

//
//  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很类似,就不多介绍了。

转载于:https://www.cnblogs.com/lishanshan/p/8465793.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值