2014-11-06 67 views
0

我有一個正在填充數據源的UITableView,而且我也能夠從數據源以及tableview本身刪除項目,所有這些工作都很好。我無法弄清楚如何在刪除之前檢查tableview單元格的文本標籤。下面是我現在正在使用的代碼,正如你現在可以看到的,我在刪除之前檢查了indexPath.row,但這不會按照我需要的方式工作。我需要的是一種讀取選定單元格的文本標籤的方法,並在我點擊刪除按鈕之前將它與if語句中的字符串進行比較。在刪除之前檢查一個單元格的textLabel

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) { 
    if (editingStyle == UITableViewCellEditingStyle.Delete) { 

     println(playersArray.count) 
     println("\(indexPath!.row) index path") 


     // check to make sure thye are not trying to delete their own points 
     if(indexPath!.row != 0) 
     { 
      println("DELETE") 
      // handle delete (by removing the data from your array and updating the tableview) 
      playersArray.removeAtIndex(indexPath!.row) 
      playersTableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic) 

      // save the new data 
      // create new array to hold the old persons array converted into NSData 
      var personEncodedObjectArray = NSKeyedArchiver.archivedDataWithRootObject(playersArray) 

      //push to NSUserDefaults 
      NSUserDefaults.standardUserDefaults().setObject(personEncodedObjectArray, forKey: "savedDataKey") 
     } 
     else 
     { 
      println("you can not delete your own points") 
     } 
    } 
+0

爲什麼不乾脆不允許他們刪除單元格,如果他們不能刪除?即不顯示他們不能刪除的單元上的刪除按鈕。 – Fogmeister 2014-11-06 16:05:01

+0

你將如何防止顯示刪除按鈕? – icekomo 2014-11-06 21:06:56

回答

0

您可以獲取該indexPath細胞並檢查其屬性:

let cell = tableView.cellForRowAtIndexPath(indexPath) as YourTableCellClass 

if cell.textLabel.text == "myText" { 
    // whatever 
} 
相關問題