2016-09-07 37 views
0

我試圖改變時,分段控制,這裏改變了我的表視圖單元格是我現在採用分段控制切換的tableView

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 
} 

我爲我的numberOfRowsInSectioncellForRowAtIndexPath

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 
} 

另外我有一個IBAction對於分割的控制

@IBAction func seg(sender: AnyObject) { 

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

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

    } 

} 

但是當我改變段時,index 0中只有一個單元出現在index 1

回答

1

在你的cellForRowAtIndexPath中,你總是返回單元格,你永遠不會返回單元格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 
} 
+0

是的,這工作謝謝你!但是,這是使用tableview使用分段控制的最佳方式嗎? –

+0

取決於你的使用案例夥計我不知道你的用例,所以我不能確定這種方法是否最好,但我可以建議一些改進,如你不需要IBAction func seg中的案例(發件人: AnyObject)只需調用一次reload就可以了,並且對於額外的獎勵,中斷行爲默認情況下是在swift中進行切換,因此如果刪除所有的break,它將工作得完全一樣。 –

+0

當細胞被挖掘時,我會遇到緩慢加載任何想法爲什麼? –