如何修复 Swift 中的 Disappearing/Invisible/Empty UITableViewCells 问题?

How to fix the Disappearing/Invisible/Empty UITableViewCells issue in Swift?

我有两个ViewControllers,一个是GoalsViewController,一个是AddNewGoalViewController

GoalsViewController 可用于删除目标(单元格)和添加新目标(单元格)。有一个 UITableView 和一个按钮, 添加新目标 。当用户按下 添加新目标 按钮时,它将传递给 AddNewGoalViewController。在 AddNewGoalViewController 中,用户将 select 锻炼、通知(他们希望收到多少次通知)以及他们想要 运行、步行或做任何其他工作的程度。

我检查了 a tutorial (click on word "tutorial" to check it), and it was helpful. The problem is that it is implementing empty cells. Download 我的项目以更好地检查它。

编辑:在花了很多时间研究您的项目后,我发现了问题。

单击您的 Main.Storyboard 文件。单击文件检查器。取消选中大小 类。完毕。你的项目成功了!

这似乎是一个 XCode 错误。如果您再次检查 Size 类,您的项目应该仍然有效。

因此,解决方法是在 Main.storyboard 文件的文件检查器中取消选中然后选中大小 类。

NONETHELESS:我的语法建议仍然有效,它使代码更清晰:

嗯,你检查过 the solution of the exercise 了吗?

页尾有一个link;)

第一个区别:

var workouts = [Workout]()

var numbers = [Workout]()

func loadSampleMeals() {
    let workouts1 = Workout(name: "Run", number: "1000")!

    let workouts2 = Workout(name: "Walk", number: "2000")!

    let workouts3 = Workout(name: "Push-Ups", number: "20")!

    workouts += [workouts1, workouts2, workouts3]
    numbers += [workouts1, workouts2, workouts3]
}

应该是:

var workouts = [Workout]()

func loadSampleMeals() {
    let workouts1 = Workout(name: "Run", number: "1000")!

    let workouts2 = Workout(name: "Walk", number: "2000")!

    let workouts3 = Workout(name: "Push-Ups", number: "20")!

    workouts += [workouts1, workouts2, workouts3]
}

第二个区别:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "DhikrTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GoalsTableViewCell

    // Fetches the appropriate meal for the data source layout.
    let dhikr = workouts[indexPath.row]
    let number = numbers[indexPath.row]


    cell.nameLabel.text = dhikr.name
    cell.numberLabel.text = number.number
    //cell.photoImageView.image = dhikr.photo
    //cell.ratingControl.rating = dhikr.rating

    return cell
}

应该是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "DhikrTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GoalsTableViewCell

    // Fetches the appropriate meal for the data source layout.
    let dhikr = workouts[indexPath.row]


    cell.nameLabel.text = dhikr.name
    cell.numberLabel.text = dhikr.number
    //cell.photoImageView.image = dhikr.photo
    //cell.ratingControl.rating = dhikr.rating

    return cell
}

P.S.:

class Workout {
    // MARK: Properties

    var name: String
    //var notifications: Int
    var number: Int

    // MARK: Initialization

    init?(name: String, number: Int) {
        // Initialize stored properties.
        self.name = name
        //self.notifications = notifications
        self.number = number

        // Initialization should fail if there is no name or if the rating is negative.
        if name.isEmpty || number < 0{
            return nil
        }
    }
}

number 永远不会 < 0,也许你的意思是 == 0?.