我是一名编码和Swift的新手,现在正在尝试创建一个小型应用程序。我创建了以下代码:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell = tableView.cellForRowAtIndexPath(indexPath)
var selectedSubject = toBeAddedSubjects[indexPath.row] as Subject
selectedSubject.name = cell.nameLabel
selectedSubject.semester = cell.semesterLabel
if cell?.accessoryType == UITableViewCellAccessoryType.Checkmark {
cell?.accessoryType = UITableViewCellAccessoryType.None;
} else {
cell?.accessoryType = UITableViewCellAccessoryType.Checkmark;
selectedCellsData.append(newElement: selectedSubject)
}
}
我现在遇到了错误:
没有更多的上下文,无法确定表达式的类型
这个错误是针对 cell.nameLabel
和 cell.semesterLabel
的。然而,在之前的代码中已经使用过它们了:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("AddSubjectCell", forIndexPath: indexPath) as! SubjectCell
let subject = Subjects[indexPath.row] as Subject
let Semester = "\(subject.semester)"
cell.nameLabel.text = subject.name
cell.semesterLabel.text = "Semester " + Semester
return cell
}
我的目标是使第一个代码末尾的附加功能起作用,因此我需要将单元格信息转换为selectedSubject
的正确类型。
var selectedCellsData = [ Subject(name: "Initial Subject", semester: 0)]