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

I have two ViewControllers , one is GoalsViewController and the other one is AddNewGoalViewController .

The GoalsViewController is useful to delete goals (cells) and to add new goals (cells). There is a UITableView and a button, Add new Goal . When the user presses the Add new Goal button, it will pass to AddNewGoalViewController . In AddNewGoalViewController users will select workout, notifications (how many times they want to be notified), and how much they want to run, walk or do any other work.

I checked a tutorial (click on word "tutorial" to check it), and it was helpful. The problem is that it is implementing empty cells. Download my project to check it better.


EDIT: After spending A LOT of time looking into your project, I found the issue.

Click on your Main.Storyboard file. Click on the file inspector. Uncheck Size Classes. Done. Your project works !

This seems to be an XCode bug. If you check again Size Classes, your project should still work.

The fix is therefore to uncheck and then check Size Classes in the File Inspector of your Main.storyboard file.

NONETHELESS: My syntax advice is still valid, it makes for cleaner code:

Well, did you check the solution of the exercise?

There is a link at the end of the page ;)

1st Difference:

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]
}

should be:

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]
}

2nd Difference:

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
}

should be:

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
}

PS:

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 will never be < 0, perhaps you meant == 0 ?.

链接地址: http://www.djcxy.com/p/33524.html

上一篇: 在c / c ++中优化和便携地转换endian

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