swift构建课程表详细代码
时间: 2023-11-12 13:13:18 浏览: 32
以下是使用 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` 方法中使用数组中的数据来配置单元格的文本。