UITableView in Swift

前端 未结 17 1381
滥情空心
滥情空心 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:20

    Here is a simple way to define table cell in swift 2:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier = "cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier) ??
            UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: identifier)
        cell.textLabel!.text = "my text"
        return cell
    }
    

    Swift 3:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier) ??
            UITableViewCell(style: .default, reuseIdentifier: identifier)
        cell.textLabel!.text = "my text"
        return cell
    }
    

提交回复
热议问题