1

I'm trying to change my table view cell when the segmented control is changed here is what I have now

internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var returnValue = 0

    switch(segmentedC.selectedSegmentIndex)
    {
    case 0:
        returnValue = rest.count
        break
    case 1:
        returnValue = fullMenu.count
        break
    default:
        break

    }

    return returnValue
}

I have this for my numberOfRowsInSection and cellForRowAtIndexPath

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("food") as! restTableViewCell
    var cell2 = tableView.dequeueReusableCellWithIdentifier("fullM") as! fullmenuTableViewCell
    switch(segmentedC.selectedSegmentIndex)
    {
    case 0:
        let res: Rest!
        res = rest[indexPath.row]

        var img: UIImage?

        if let urls = foo.imageString{
            img = foodViewController.imageCache.objectForKey(urls) as? UIImage

        }
        dispatch_async(dispatch_get_main_queue()) {
            cell.setupViews(res)

        }

        break
    case 1:
            cell2.textLabel?.text = fullMenu[indexPath.row]
        break

    default:
        break

    }
    return cell
}

Also I have an IBAction for the segmented control

@IBAction func seg(sender: AnyObject) {

    switch(segmentedC.selectedSegmentIndex)
    {
    case 0:
        tableView.reloadData()
        break

    case 1:
        tableView.reloadData()
        break
    default:
        break

    }

}

But when I change the segment only one cell from the index 0 show up in the index 1

1 Answer 1

2

In your cellForRowAtIndexPath you are always returning cell you are never returning cell2. Try returning cell2 in your second case.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("food") as! restTableViewCell
var cell2 = tableView.dequeueReusableCellWithIdentifier("fullM") as! fullmenuTableViewCell
switch(segmentedC.selectedSegmentIndex)
{
case 0:
    let res: Rest!
    res = rest[indexPath.row]

    var img: UIImage?

    if let urls = foo.imageString{
        img = foodViewController.imageCache.objectForKey(urls) as? UIImage

    }
    dispatch_async(dispatch_get_main_queue()) {
        cell.setupViews(res)

    }

    break
case 1:
        cell2.textLabel?.text = fullMenu[indexPath.row]
        return cell2
    break

default:
    break

}
return cell
}
Sign up to request clarification or add additional context in comments.

7 Comments

Yes this worked thank you!. But is this the best way to use segmented control with tableviews?
Depends on your use case dude i don't know your use case so i can't be sure if this approach is best or not but i can suggest some improvements like you don't need cases in your IBAction func seg(sender: AnyObject) just calling reload once will do, and for an added bonus the break behaviour is by default for switch in swift so if you remove all the break it will work exactly the same.
Am experiencing slow loading when cell is tapped any idea why?
Can you share your didSelectRowAtIndexPath code? Might be some issue there.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var foo:Food! foo = food[indexPath.row] var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! selectedCell.contentView.backgroundColor = UIColor.clearColor() performSegueWithIdentifier("Vc", sender: foo) }
|

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.