Ios 使用分段控件切换tableView

Ios 使用分段控件切换tableView,ios,swift,uitableview,uisegmentedcontrol,Ios,Swift,Uitableview,Uisegmentedcontrol,当分段控件被更改时,我试图更改我的表视图单元格,这就是我现在所拥有的 internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { var returnValue = 0 switch(segmentedC.selectedSegmentIndex) { case 0: returnValue = rest.count

当分段控件被更改时,我试图更改我的表视图单元格,这就是我现在所拥有的

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
}
我的
numberofrowsinssection
cellforrowsatindexpath

 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
}
我还有一个用于分段控件的
iAction

@IBAction func seg(sender: AnyObject) {

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

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

    }

}

但是当我更改段时,只有
索引0
中的一个单元格出现在
索引1

单元格中。在单元格中,您总是返回单元格,而从不返回单元格2。在第二种情况下,尝试返回cell2

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
}

是的,这很有效,谢谢!。但是这是在表视图中使用分段控件的最佳方法吗?取决于你的用例,伙计我不知道你的用例,所以我不能确定这种方法是否最好,但我可以建议一些改进,比如你不需要在IBAction函数seg(发送者:AnyObject)中使用案例,只需调用一次重载即可,另外,swift中的切换默认情况下会出现中断行为,因此如果删除所有中断,其工作方式将完全相同。当点击单元格时,我的加载速度很慢。知道原因吗?能否共享您的didSelectRowAtIndexPath代码?可能存在一些问题。
func tableView(tableView:UITableView,didselectRowatineXpath-indexPath:NSIndexPath){var foo:foo!foo=Food[indexPath.row]var selectedCell:UITableViewCell=tableView.CellForRowatineXpath(indexath)!selectedCell.contentView.backgroundColor=UIColor.clearColor()performsguewithidentifier(“Vc”,发件人:foo)}