Swift纯代码构建VC

纯代码构建VC

以IOS13之后举例(IOS13要用SceneDelegate)

  1. SceneDelegate的scene(willConnectTo)重载方法:
if let windowScene = scene as? UIWindowScene {
    self.window = UIWindow(windowScene: windowScene)
    self.window?.rootViewController = TestOnlyCodeViewController()
    self.window?.backgroundColor = UIColor.white//不设置就是黑的
    self.window?.makeKeyAndVisible()
}
  1. 创建TestOnlyCodeViewController,不创建对应xib:
class TestOnlyCodeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let screen = UIScreen.main.bounds
        let labelWidth:CGFloat = 90
        let labelHeight:CGFloat = 20
        let labelTopView:CGFloat = 150
        let frame = CGRect(x: (screen.size.width - labelWidth)/2, y: labelTopView, width: labelWidth, height: labelHeight)
        let label = UILabel(frame: frame)
        label.text = "test only code show view"
        
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Swift 构建课程表的示例代码: ```swift import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! let courses = ["Math", "Science", "History", "English"] let days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] let times = ["9:00am - 10:00am", "10:15am - 11:15am", "11:30am - 12:30pm", "1:00pm - 2:00pm", "2:15pm - 3:15pm"] override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self tableView.delegate = self } // MARK: - Table view data source func numberOfSections(in tableView: UITableView) -> Int { return days.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return times.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return days[section] } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) // Configure the cell... let course = courses[indexPath.row % courses.count] let time = times[indexPath.row] cell.textLabel?.text = "\(time) - \(course)" return cell } } ``` 该示例代码使用了一个数组来存储课程、日期和时间,然后在 `numberOfSections` 方法中返回日期的数量,在 `numberOfRowsInSection` 方法中返回时间的数量。在 `titleForHeaderInSection` 方法中设置日期标题,然后在 `cellForRowAt` 方法中使用数组中的数据来配置单元格的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值