2016-03-11 58 views
0

我是新來的swift善良需要任何指針。 我有一個像下面的表視圖控制器中的數組。而且我在使用細胞表標籤在的cellForRowAtIndexPathIOS Swift Tableview: - 如何根據內容顯示單元格?

var arrTest: [String] = ["test", "test2", "test3", "try", "test4", "try", "test5", "try"] 

cell.textLabel?.text = arrTest[indexPath.row] 

其工作只是正常顯示的字符串。但是我想跳到陣列中的下一個項目,每次我點擊「嘗試」。我嘗試了下面的步驟,但無法達到我想要的效果。任何指針?

var tempIndex: Int = 0 

的cellForRowAtIndexPath

if tempIndex < arrTest.count && arrTest[tempIndex] != "try" { 
     cell.textLabel?.text = arrTest[tempIndex] 
     tempIndex++ 
    } else { 
     tempIndex++ 
     repeat{ 
     if tempIndex < arrTest.count && arrTest[tempIndex] != "try" { 
       cell.textLabel?.text = arrTest[tempIndex] 
        tempIndex++ 
       break 
      } 
      tempIndex++ 
     } while tempIndex < arrTest.count 

    } 
+0

你的意思是你要顯示與細胞只有以下標籤?:「test」,「test2」,「test3」,「test4」,「test5」 – bcamur

+1

當您獲取數組時,您可以從「try」表格陣列和重新加載tableView –

+0

是的,只有「test」,「test2」,「test3」,「test4」,「test5」 任何時候「嘗試」出現我必須跳到下一個項目。我不介意桌子末端的空白單元格。 – vanquish

回答

1

簡單的方式不顯示此細胞

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    if arrTest[indexPath.row] == "try" 
    { 
     return 0 
    } 
    return 44 
} 

如果你的高度動態然後用這個

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    if arrTest[indexPath.row] == "try" 
    { 
     return 0 
    } 
    return UITableViewAutomaticDimension 
} 

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    return 44.0 
} 
0

卸下陣列 「嘗試」,然後填寫表格。

let newArrayTest = arrTest.filter() {$0 != "try"} 
yourTableView.reload(); 
相關問題