2015-07-12 33 views
2

我試圖解決的問題是爲已加載的任務返回多個單元格,因爲我無法在未加載全新任務的情況下進行迭代。在TableView中返回多個dequeueReusableCells Swift

每個視圖都有一個或多個應該爲特定任務加載的單元格,而且我似乎無法解決問題。感謝您的幫助!

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

    let categoryCells = self.tasks[indexPath.row] as? Task 
    var cellChooser: BoredTaskCell! 

    if selectedTaskCategory == "Movie"{ 
    let cell = tableView.dequeueReusableCellWithIdentifier("imdbCell") as! BoredTaskCell! 
     cell!.selectionStyle = UITableViewCellSelectionStyle.None 
     cellChooser = cell 
    } 
    else if selectedTaskCategory == "Lifestyle" { 
     let emailCell = tableView.dequeueReusableCellWithIdentifier("emailCreatorCell") as! BoredTaskCell! 
     emailCell!.selectionStyle = UITableViewCellSelectionStyle.None 
     cellChooser = emailCell 

    } 
    else { 
     let emptyCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! BoredTaskCell! 
     cellChooser = emptyCell 
     emptyCell!.selectionStyle = UITableViewCellSelectionStyle.None 
    } 

    return cellChooser 
} 
+0

你在哪裏設置了'selectedTaskCategory'?它是全局變量嗎? –

+0

是的,這是全球性的! – LucasGron

+0

然後,在分配新值後,您只需重新加載tableView以顯示更新的單元格。 –

回答

3

簡短的回答是,你不能從一次調用返回兩個單元格到cellForRowAtIndexPath。但是爲了實現你想要的,改變你構造tableView的方式,所以每個任務都有一個部分,而不是每個任務的一個單元。即使用indexPath.section作爲陣列的索引。您將需要修改所有表格視圖數據源方法(例如,所以numberOfSectionsInTableView將返回self.tasks.count)。技巧來自numberOfRowsInSection,通常應該返回1,但對於需要兩個單元的任務,它應該返回2.然後在cellForRowAtIndexPath中,確定使用indexPath.section顯示哪個任務。然後,如果indexPath.row爲0,則使用一種單元格類型(例如imdbcell),如果它是1,則使用其他單元格類型(例如消息單元格)。