【问题标题】:How to fix the Disappearing/Invisible/Empty UITableViewCells issue in Swift?如何解决 Swift 中的消失/不可见/空 UITableViewCells 问题?
【发布时间】:2016-08-08 07:20:02
【问题描述】:

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

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

我检查了a tutorial(单击“教程”一词进行检查),这很有帮助。问题是它正在实现空单元格。 Download我的项目以更好地检查它。

【问题讨论】:

  • 真的没有得到你的问题。您在 tableview 或 addgoals 上遇到什么问题,问题是什么
  • 有问题吗?哪个问题?
  • 您应该显示您的代码(可以在 google drive 中上传并提供链接)并准确说明您面临的问题。
  • 即使下载了代码我仍然无法理解你的问题。您尚未在goalViewController 中为tableView 添加任何数据源或委托。同样在 AddNewGoalsViewController 中,您还没有为 UIButton 的操作添加任何代码。你想向我们解释什么?能更新一下吗?
  • 我尝试查看您的代码,但缺少太多信息。需要你的故事板来理解你需要什么。

标签: ios swift uitableview uitextfield uipickerview


【解决方案1】:

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

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

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

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

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

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

页面末尾有一个链接;)

第一个区别:

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
}

附:

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?。

【讨论】:

  • @EminEmini 很高兴为您提供帮助^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 2020-09-28
  • 2015-01-30
相关资源
最近更新 更多