1

I can't call array in the tableView delegate method cellForRow at indexPath. it gives an error message:

Cannot assign value of type string to typeString?

import UIKit

struct cellData {
    var open = Bool()
    var currentRides = [String]()
    var RequestedRides = [String]()
    var sectionData = [String]()
}

class RideResultViewController: ContentViewController,  UITableViewDelegate, UITableViewDataSource {
    let currentRidesArray = ["1", "2", "3"]
    let RequestedRidesArray = ["A", "B", "C"]

    var tableViewData = [cellData]()

    @IBAction func segmentedValueChanged(_ sender: Any) {
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //Expandable cell initailization
        tableViewData = [cellData(open: false, currentRides: currentRidesArray, RequestedRides: RequestedRidesArray, sectionData: ["cell1", "cell2", "cell3"])]
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
        if segmentedControl.selectedSegmentIndex == 0 {
            cell.textLabel?.text =   tableViewData[indexPath.section].currentRides
        } else if segmentedControl.selectedSegmentIndex == 1 {
            cell.textLabel?.text =    RequestedRidesArray[indexPath.row]
        }

        return cell
    } else {
        guard let cell =     tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
        if segmentedControl.selectedSegmentIndex == 0 {
            cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row]
        } else if segmentedControl.selectedSegmentIndex == 1 {
            cell.textLabel?.text =    tableViewData[indexPath.section].sectionData[indexPath.row]
        }

        return cell
    }
}

The code not building. I tied to put it in a square bracket but it still gave same error.

6
  • tableViewData[indexPath.section].currentRides => tableViewData[indexPath.section].currentRides[indexPath.row]? tableViewData[indexPath.section].currentRides is an Array of String (ie [String]), not a String Commented May 21, 2019 at 11:58
  • Could you please specify which array are you trying to call that is giving you the compiler error and if you could point out the line also that is giving you the error it would help in understanding the problem better Commented May 21, 2019 at 11:59
  • You can not assign array to label title instead of string because both data type is different. Commented May 21, 2019 at 12:01
  • Thanks @Larme your suggestions worked Commented May 21, 2019 at 12:09
  • @Nimesh Your suggestions worked thanks Commented May 21, 2019 at 12:09

2 Answers 2

2

Its because this is [String] an array of String

Change this line to

cell.textLabel?.text =   tableViewData[indexPath.section].currentRides

This line

cell.textLabel?.text =   tableViewData[indexPath.section].currentRides[indexPath.row]
Sign up to request clarification or add additional context in comments.

1 Comment

@Diran Could you then accept an answer for this question? Thanks :)
2

The error is self explainary..

Cannot assign value of type '[String]' to type 'String?'

It means you are assigning an array of string i.e. [String] to string type which is not allowed.

When you are setting cell.textLabel?.text = ... please make sure that you are assigning String type not [String] type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.