【问题标题】:Display Realm List data in UITableView在 UITableView 中显示 Realm List 数据
【发布时间】:2019-12-08 22:49:39
【问题描述】:

这是我的三个对象:

锻炼.swift:

@objc dynamic var id = 0
@objc dynamic var date: Date?
// List of exercises (to-many relationship)
var exercises = List<Exercise>()

override static func primaryKey() -> String? {
    return "id"
}

Exercise.swift

class Exercise: Object {

    @objc dynamic var name: String?
    // List of sets (to-many relationship)
    var sets = List<Set>()
    var parentWorkout = LinkingObjects(fromType: Workout.self, property: "exercises")
}

设置.swift

class Set: Object {

    @objc dynamic var reps: Int = 0
    @objc dynamic var kg: Double = 0.0
    @objc dynamic var notes: String?
    // Define an inverse relationship to be able to access your parent workout for a particular set (if needed)
    var parentExercise = LinkingObjects(fromType: Exercise.self, property: "sets")

    convenience init(numReps: Int, weight: Double, aNote: String) {
       self.init()
       self.reps = numReps
       self.kg = weight
       self.notes = aNote
    }
}

这就是我从 Realm 获得价值的方式:

var data:Results<Workout>!
var result:[Workout] = []
func retreiveData() { // Retreive data from Realm

    let realm = try! Realm()

    self.data = realm.objects(Workout.self)
    self.result = Array(self.data)
}

我正在使用CVCalendar,这就是我每天锻炼的方式:

func didSelectDayView(_ dayView: DayView, animationDidFinish: Bool) { // When select date in dayView

    var calendar = Calendar.current
    calendar.locale = Locale.current
    calendar.timeZone = TimeZone(identifier:"GMT")!
    let date = dayView.date.convertedDate(calendar: calendar)!

    let res = result.filter { compareDate(date1:$0.date!, date2:date) }

    self.resultFromDayViewDate = res
    print("resultFromDayViewDate: \(resultFromDayViewDate)")

    myTableView.reloadData()
}

由于今天注册了三个练习,输出给我:

[Workout {
    id = 0;
    date = 2019-12-08 17:39:32 +0000;
    exercises = List<Exercise> <0x6000006ad320> (
        [0] Exercise {
            name = Barbell Biceps Curl;
            sets = List<Set> <0x6000006b50e0> (
                [0] Set {
                    reps = 1;
                    kg = 4;
                    notes = D;
                }
            );
        },
        [1] Exercise {
            name = Barbell Squat;
            sets = List<Set> <0x6000006b5200> (
                [0] Set {
                    reps = 1;
                    kg = 66;
                    notes = Fffd;
                }
            );
        },
        [2] Exercise {
            name = Cable Fly;
            sets = List<Set> <0x6000006b5290> (
                [0] Set {
                    reps = 1;
                    kg = 4;
                    notes = Df;
                },
                [1] Set {
                    reps = 1;
                    kg = 44;
                    notes = Gege;
                }
            );
        }
    );
}]

现在,我尝试在UITableView 中展示这些练习。既然有三个练习,那么应该有三个单元格。我只是想显示练习名称。这是我到目前为止所得到的:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    //return myWorkout.exercises.count
    return resultFromDayViewDate[section].exercises.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle,
                           reuseIdentifier: "cell")

    let workouts = resultFromDayViewDate[0].exercises

    for exe in workouts {
        cell.textLabel?.text = "Exercise: \(exe.name)"
    }

    return cell
}

但这只显示最后一个练习,而不是全部三个?我该怎么做?

【问题讨论】:

  • 嘿伙计,请不要在您的代码问题中包含“Xcode”。仅包含 Xcode 相关问题的“Xcode” :)
  • 您提出了 8 个彼此相似的问题。虽然我们不介意提供帮助,但您可能会从任何可用的在线教程中受益。 Ray Wenderlich 有一些很棒的教程,涵盖了您的问题 NavigationInfinite Scrolling 非常棒。这个来自 Apple 的文章涵盖了编辑和删除行为 UITableView

标签: ios swift realm


【解决方案1】:

替换

let workouts = resultFromDayViewDate[0].exercises

for exe in workouts {
    cell.textLabel?.text = "Exercise: \(exe.name)"
}

cell.textLabel?.text = "Exercise: \(resultFromDayViewDate[indexPath.section].exercises[indexPath.row].name)"

【讨论】:

  • 另外,摆脱cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell"),因为您已经将单元格出列 - 不需要(也不应该)创建一个新对象。
  • 如果根本没有添加任何练习,它会在return resultFromDayViewDate[section].exercises.count 崩溃并显示错误消息:Thread 1: Fatal error: Index out of range。知道如何解决这个问题吗?
  • @ChrisShaw ^ 你知道吗?
  • 如果你没有实现func numberOfSections(in: UITableView) -&gt; Int,我认为节数默认为1,然后你的代码将遵循resultFromDayViewDate[0]。实现numberOfSections函数并使其成为return resultFromDayViewDate.count
  • @ChrisShaw 我的UITableView 只有一个部分,但有多行。正如你在numberOfRowsInSection 中所说,我添加了return resultFromDayViewDate.count,但看起来它返回了锻炼次数,但我希望它返回锻炼次数
猜你喜欢
  • 2012-08-10
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
相关资源
最近更新 更多