user2695433
user2695433

Reputation: 2163

Using multiple UITableview in Single ViewController with Custom TableViewCell

How can I return a TableViewcell in cellForRowAtIndexPath delegate method , if I m using multiple UITableView in a Single ViewController, One TableView has Custom UITableViewCell and the other has default UITableViewCell . My problem is I m not able to cast the UITableViewCell to Custom TableViewCell type .

Code used as follows ,

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

        var cell:UITableViewCell?
        if tableView == self.tvCity {
            var cell:UITableViewCell?
            cell = tableView.dequeueReusableCellWithIdentifier(cityCellIdentifier, forIndexPath: indexPath) as UITableViewCell

            let row = indexPath.row
            cell!.textLabel?.text = self.cityList[row].cityEn
                   }
        if tableView == self.tvBranchByCity {
            var cell:BranchNearMeTableViewCell?
            cell = (tableView.dequeueReusableCellWithIdentifier(branchCellIdentifier, forIndexPath: indexPath) as! BranchNearMeTableViewCell)

            let row = indexPath.row
           cell.branchName = self.branchList[row].name// here cell.branchName is not accessible.

        }

        return cell!
    }

Please advise . Thanks in advance.

Upvotes: 0

Views: 93

Answers (2)

Kavita
Kavita

Reputation: 176

Differentiate tables with tag

see example :

if tableView.tag == 2000

 {

identifier = "First Table"

 let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)

 return cell

 }

else

  {

 identifier = "Second Table"

let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)

 return cell

 }

Upvotes: 1

Yagnesh Dobariya
Yagnesh Dobariya

Reputation: 2251

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


        if tableView == self.tvCity {
            var cell:UITableViewCell?
            cell = tableView.dequeueReusableCellWithIdentifier(cityCellIdentifier, forIndexPath: indexPath) as UITableViewCell

            let row = indexPath.row
            cell!.textLabel?.text = self.cityList[row].cityEn
            return cell!
                   }
        if tableView == self.tvBranchByCity {
            var cell:BranchNearMeTableViewCell?
            cell = (tableView.dequeueReusableCellWithIdentifier(branchCellIdentifier, forIndexPath: indexPath) as! BranchNearMeTableViewCell)

            let row = indexPath.row
           cell.branchName = self.branchList[row].name as! String // here cell.branchName is not accessible.
           return cell!
        }


    }

Upvotes: 1

Related Questions