2015-12-14 30 views
0

默認情況下,每個部分應顯示最多三個單元格。如果任何單元格包含多於三個單元格,則應該顯示'show more'選項。如果show more被點擊,我想在該特定部分中顯示其餘單元格。我花了兩天時間,沒有什麼能解決的。桌子頂部的部分。取決於選擇的段,tableview加載單元格。這段代碼對於每個段每段都不相同,所以func cellForRowAtIndexPath:變得很大很大。我大致添加了代碼。這段代碼就是我試過的。Display在uitableview中顯示更多

if segmentName == .Feature || segmentName == .Services 
    { 
     if indexPath.section == 0 
     { 
      if boolShowFullFeature[indexPath.section] == false 
      { 
       if indexPath.row == showCells 
       { 
        return createShowMoreCell(indexPath.section) 
       } 
       else 
       { 
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SearchListViewCell 
        var firstTitle = "", secondTitle = "", thirdTitle = "", recomendValue = "", starValue = "" 

        (firstTitle, secondTitle, thirdTitle, recomendValue, starValue) = model.foodValue[indexPath.row] 
        cell.configureCell(firstTitle, secondTitle: secondTitle, thirdTitle: thirdTitle, recomendValue: recomendValue, starValue: starValue) 
        return cell 
+1

這是一些有趣的信息。你也有問題嗎? – dasdom

+0

@Antony你試過了什麼?什麼工作?什麼沒有? – kostek

+0

@dasdom。好方法通知我.. :-) – Antony

回答

0

我以前實際上已經這樣做過。這裏是示例代碼:

var objects = [["1","2","3"],["q","w","e","r","t","y"],["z","x","c","v","b","n","m"]] 
var sec = ["sec a","sec b","sec c"] 
var showallSec = 0 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return sec.count 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let items = objects[section].count 

    if items > 3{ 
     if section == showallSec-1{ 
      return items 
     } 
     return 4 
     } 
    return items 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

    let object = objects[indexPath.section][indexPath.row] 
    cell.textLabel!.text = object 
    if(indexPath.section != showallSec-1){ 
    if(indexPath.row == 3){ 
     cell.textLabel!.text = "show more" 
     }} 
    return cell 
} 

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return sec[section] 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if(indexPath.row == 3){ 
     showallSec = indexPath.section + 1 
     tableView.reloadData() 
    } 
}