4

I am new to coding and Swift and am now trying to create a small app. I created this part of code:

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

I am now getting the error

Type of expression is ambiguous without more context

for cell.nameLabel and cell.semesterLabel. These, however, have already been used in a previous piece of code:

 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

}

It is my goal to make the append function at the end of the first code work, thus, I need to transform the cell information into the correct type for selectedSubject.

var selectedCellsData = [ Subject(name: "Initial Subject", semester: 0)]
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Elina
  • 79
  • 8
  • 1
    Welcome to SO and nice first post. As an FYI, I added some minor formatting changes to your inline code (the custom on SO is to use backticks (the key next to the 1 on a US keyboard). My edits won't appear until they have been reviewed. – Richard Erickson Aug 03 '15 at 16:49

1 Answers1

1

It looks like you are casting your cell as a SubjectCell in the second working code sample, but not in the problem code. Make sure you cast the cell to your custom class so you can access your custom properties!

let cell = tableView.cellForRowAtIndexPath(indexPath) as! SubjectCell

Also, you are assigning UILabels to the name and semester properties of your Subject. Do you mean to assign the text of those labels?

selectedSubject.name = cell.nameLabel.text
selectedSubject.semester = cell.semesterLabel.text
jrisberg
  • 764
  • 4
  • 16
  • Thank you! I now changed the casting. Yes, that is exactly what I am trying to do, though the second label is an Int. My current problem is the append function. The sense behind it all is that the selectedSubject data flow into a new dataset and I want to integrate this data set into another. This I am now trying to do with 'subjectsData.append(selectedCellsData)'. The 'subjectsData' dataset has the same structure as 'selectedCellsData' (structure posted above). But I always receive the error "Cannot invoke 'append' with an argument list of type '([Subject])'. Do you have an idea what's to do? – Elina Aug 04 '15 at 07:27
  • In addition to my first comment, the updated code to collect the data for selectedCellsData: 'selectedSubject.name = cell.nameLabel.text!' and 'selectedSubject.semester = cell.semesterNumberLabel.text!.toInt()!' – Elina Aug 04 '15 at 07:30
  • It sounds like `subjectsData` and `selectedCellsData` may not be compatible types. What are the types of these? Where are they defined? – jrisberg Aug 04 '15 at 15:36
  • Make sure that your array is a Mutable array, otherwise you won't be able to add to it – jrisberg Aug 04 '15 at 15:41
  • `var subjectsData = [ Subject(name: "Investments", semester: 1), Subject(name: "Statistics", semester: 1), Subject(name: "Studium Universale", semester: 2) ]` and `var selectedCellsData = [ Subject(name: "Initial Subject", semester: 0)]` – Elina Aug 04 '15 at 16:06
  • Those are both normal NSArrays, you need NSMutableArrays to allow for appending. Try declaring them as NSMutableArrays and building them that way – jrisberg Aug 04 '15 at 17:54
  • But aren't they already mutable due to the `var`? NSMutableArray is objective c, isn't it? So this is not applied to Swift as Swift already includes mutable in `var`or did I misunderstood this? – Elina Aug 08 '15 at 15:25
  • You still need to declare it as an NSMutableArray I believe. Var makes it a variable while let makes it a constant, but that doesn't affect the type of the property. – jrisberg Aug 10 '15 at 16:10
  • according to this link ( http://stackoverflow.com/questions/24096096/immutable-mutable-collections-in-swift ) `var` is already the swift equivalent to `NSMutableArray *myArray`. – Elina Aug 11 '15 at 12:57
  • Well I am mistaken. Sorry about that, guess I need to do some reading myself. – jrisberg Aug 11 '15 at 19:08
  • 1
    I would suggest making a separate question or updating for just the append issue you're having, since at least part of your problem was solved. That way it's more focused, it's technically a separate issue. – jrisberg Aug 11 '15 at 19:10