bashayr.m
bashayr.m

Reputation: 5

identify many cell in uitable view

I want to identify many custom cell in table view "i build them in storyboard " but the are error ask from me to return value , i am trying to return nil and int value and cell but the error be same

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->  UITableViewCell {
    if indexPath == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("CheefsCell", forIndexPath: indexPath) as UITableViewCell
            cell.accessoryType = .DisclosureIndicator
        return cell }
    else if indexPath == 1 {

        let cell = tableView.dequeueReusableCellWithIdentifier("BeautyCell", forIndexPath: indexPath) as UITableViewCell
            cell.accessoryType = .DisclosureIndicator
            return cell }
    else if indexPath == 2 {
        let cell = tableView.dequeueReusableCellWithIdentifier("StudentServicesCell", forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = .DisclosureIndicator
        return cell }
    else if indexPath == 3 {
        let cell = tableView.dequeueReusableCellWithIdentifier("ArtAndDesigneCell", forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = .DisclosureIndicator
        return cell }
    else if indexPath == 4 {
        let cell = tableView.dequeueReusableCellWithIdentifier("StoreCell", forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = .DisclosureIndicator
        return cell }
    else if indexPath == 5 {
        let cell = tableView.dequeueReusableCellWithIdentifier("OthersCell", forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = .DisclosureIndicator
        return cell }
    return

}

update :: git hub link /

Upvotes: 0

Views: 60

Answers (3)

nielsbot
nielsbot

Reputation: 16022

Another example based on @vadian's answer:

static let identifiers = [ "CheefsCell", "BeautyCell", "StudentServicesCell", "ArtAndDesigneCell", "StoreCell" ]

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

    let identifier = (indexPath.row < count) ? identifiers[ indexPath.row ] : "OthersCell"
    var cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
    if cell == nil {
        cell = UITableViewCell() // allocate a new cell here
    }

    cell.accessoryType = .DisclosureIndicator
    // configure your cell here

    return cell
}

Upvotes: 0

vadian
vadian

Reputation: 285079

The method cellForRowAtIndexPath requires to return a non-optional UITableViewCell, so you must ensure to return a cell in any case. A simple return or return nil is not allowed.

The code can be simplified, most of it is redundant. The only difference is the identifier.

A suitable solution is a switch statement on the row property of the indexpath.

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

  var identifier : String
  switch indexPath.row {
  case 0: identifier = "CheefsCell"
  case 1: identifier = "BeautyCell"
  case 2: identifier = "StudentServicesCell"
  case 3: identifier = "ArtAndDesigneCell"
  case 4: identifier = "StoreCell"
  default:  identifier = "OthersCell"
  }
  let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
  cell.accessoryType = .DisclosureIndicator
  return cell
}

Upvotes: 1

Rafał Sroka
Rafał Sroka

Reputation: 40030

You're getting an error cause you're not returning a cell from the method.

  1. Change your code so it always returns a cell.
  2. When checking indexPaths, use their row property - indexPath.row.
  3. Consider replacing your if-else tree with switch.

Upvotes: 0

Related Questions