UITableView in Swift

前端 未结 17 1416
滥情空心
滥情空心 2020-12-04 08:09

I\'m struggling to figure out what\'s wrong with this code snippet. This is currently working in Objective-C, but in Swift this just crashes on the first line of the method.

17条回答
  •  生来不讨喜
    2020-12-04 08:40

    UITableView Demo using Playground

    //: Playground - noun: a place where people can play
    
    import UIKit
    import PlaygroundSupport
    
    class TableviewDemoDelegate:NSObject,UITableViewDataSource,UITableViewDelegate {
    
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 100
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
    
            if cell == nil {
                cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            }
    
    
            cell?.textLabel?.text = "Item \(indexPath.row+1)"
    
            return cell!
        }
    
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            print("You have selected cell #\(indexPath.row)!")
    
        }
    }
    
    var tableView = UITableView(frame:CGRect(x: 0, y: 0, width: 320, height: 568), style: .plain)
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    
    let delegate = TableviewDemoDelegate()
    tableView.delegate = delegate
    tableView.dataSource = delegate
    
    
    PlaygroundPage.current.liveView = tableView
    

提交回复
热议问题