如何解决Swift中的消失/不可见/空UITableViewCells问题?

Emm*_*Emm 8 uitableview uipickerview uitextfield ios swift

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

GoalsViewController对删除目标(单元格)和添加新目标(单元格)很有用.有UITableView一个按钮,添加新目标.当用户按下添加新目标按钮时,它将传递给AddNewGoalViewController.在AddNewGoalViewController用户中,将选择锻炼,通知(他们希望被通知的次数),以及他们想要跑步,走路或做任何其他工作的程度.

我检查了一个教程(点击"教程"一词来检查它),这很有帮助.问题是它正在实现空单元格.下载我的项目以更好地检查它.

Cod*_*000 3

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

单击您的 Main.Storyboard 文件。单击文件检查器。取消选中尺寸类别。完毕。你的项目有效!

这似乎是一个 XCode 错误。如果您再次检查 Size Classes,您的项目应该仍然可以工作。

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

尽管如此:我的语法建议仍然有效,它使代码更清晰:

嗯,你检查过练习的答案吗?

页面末尾有一个链接;)

第一个区别:

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]
}
Run Code Online (Sandbox Code Playgroud)

应该:

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]
}
Run Code Online (Sandbox Code Playgroud)

第二个区别:

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
}
Run Code Online (Sandbox Code Playgroud)

应该:

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
}
Run Code Online (Sandbox Code Playgroud)

附:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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