Luis Ibarra
Luis Ibarra

Reputation: 1

Use of unresolved identifier 'cell'

I am creating multiple UITableViews in one view controller, and i have used the following code to check for which tableView it is. I keep getting the problem "Use of unresolved identifier 'cell' in the very last line. I have tried searching for this, but there are not many which are up to date at Xcode 7. Forgive me for my lack of terminology as well as experience, for i am just starting off. Please feel free to ask me of any questions that you might encounter. Thank you all in advance who took the time to read this!

@IBOutlet var tableView1: UITableView!
@IBOutlet var tableView2: UITableView!

// Mens games Variables
var dates = ["7/23", "7/24","7/25","7/26","7/27","7/28","7/29"]
var times = ["7:30","8:00","8:30","9:00","9:30","10:00","10:30"]
var teamsOne = ["VU Reserves 1","VU Reserves 3","VU Reserves 5","VU Reserves 7","VU Reserves 9","VU Reserves 11","VU Reserves 13"]
var teamsTwo = ["VU Reserves 2","VU Reserves 4","VU Reserves 6","VU Reserves 8","VU Reserves 10","VU Reserves 12","VU Reserves 14"]
var fields = ["Turf","Field 1","Field 2","Field 3","Field 4","Field 5","Field 6"]


// Womens games Variables
var womensDates = ["7/23", "7/24","7/25","7/26","7/27","7/28","7/29"]
var womensTimes = ["7:30","8:00","8:30","9:00","9:30","10:00","10:30"]
var womensTeamsOne = ["VU Girls 1","VU Girls 3","VU Girls 5","VU Girls 7","VU Girls 9","VU Girls 11","VU Girls 13"]
var womensTeamsTwo = ["VU Girls 2","VU Girls 4","VU Girls 6","VU Girls 8","VU Girls 10","VU Girls 12","VU Girls 14"]
var womensFields = ["Turf","Field 1","Field 2","Field 3","Field 4","Field 5","Field 6"]




override func viewDidLoad() {
    super.viewDidLoad()

    tableView1.dataSource = self
    tableView1.delegate = self
    tableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell1")
    tableView2.dataSource = self
    tableView2.delegate = self
    tableView2.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2")
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    if (tableView == tableView1) {
        return teamsOne.count
    }
    else if (tableView == tableView2) {

    return womensTeamsOne.count

    } else{

        return 0 }

    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    if (tableView == tableView1) {

    let cell = self.tableView1.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! gamesCustomCell

    cell.date.text = dates[indexPath.row]
    cell.time.text = times[indexPath.row]
    cell.teamOne.text = teamsOne[indexPath.row]
    cell.teamTwo.text = teamsTwo[indexPath.row]
    cell.field.text = fields[indexPath.row]

        return cell

    } else if (tableView == tableView2) {


        let cell = self.tableView2.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! womensGamesCustomCell

        cell.womensDate.text = womensDates[indexPath.row]
        cell.womensTime.text = womensTimes[indexPath.row]
        cell.womensTeamOne.text = womensTeamsOne[indexPath.row]
        cell.womensTeamTwo.text = womensTeamsTwo[indexPath.row]

     return cell

    }

    return cell
  }
  }

Upvotes: 0

Views: 1793

Answers (2)

Hieu Dinh
Hieu Dinh

Reputation: 722

Try this, it worked for me

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  if (tableView == tableView1) {
    let cell = self.tableView1.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! gamesCustomCell

    cell.date.text = dates[indexPath.row]
    cell.time.text = times[indexPath.row]
    cell.teamOne.text = teamsOne[indexPath.row]
    cell.teamTwo.text = teamsTwo[indexPath.row]
    cell.field.text = fields[indexPath.row]

    return cell

  } else if (tableView == tableView2) {
    let cell = self.tableView2.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! womensGamesCustomCell

    cell.womensDate.text = womensDates[indexPath.row]
    cell.womensTime.text = womensTimes[indexPath.row]
    cell.womensTeamOne.text = womensTeamsOne[indexPath.row]
    cell.womensTeamTwo.text = womensTeamsTwo[indexPath.row]

    return cell
  }

  return UITableViewCell()
}

Upvotes: 0

Chandan
Chandan

Reputation: 757

Just replace cellForRowAtIndexPath with below code

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell?

if (tableView == tableView1) {

cell = self.tableView1.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! gamesCustomCell

cell.date.text = dates[indexPath.row]
cell.time.text = times[indexPath.row]
cell.teamOne.text = teamsOne[indexPath.row]
cell.teamTwo.text = teamsTwo[indexPath.row]
cell.field.text = fields[indexPath.row]

}
 if (tableView == tableView2) {

    cell = self.tableView2.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! womensGamesCustomCell

    cell.womensDate.text = womensDates[indexPath.row]
    cell.womensTime.text = womensTimes[indexPath.row]
    cell.womensTeamOne.text = womensTeamsOne[indexPath.row]
    cell.womensTeamTwo.text = womensTeamsTwo[indexPath.row]

}

return cell!
}

Upvotes: 1

Related Questions