删除前检查单元格的textLabel

时间:2014-11-06 15:43:19

标签: xcode uitableview swift

我有一个用数据源填充的UITableView,我也可以从数据源和tableview本身删除项目,一切正常。我无法弄清楚的是如何在删除之前检查tableview单元格的textlabel。下面是我现在的代码,你现在可以看到我在删除之前检查indexPath.row,但这不会像我需要的那样工作。我需要的是一种方法来读取所选单元格的文本标签,并在我点击删除按钮之前将其与if语句中的字符串进行比较。

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")
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以获取该indexPath的单元格并检查其属性:

let cell = tableView.cellForRowAtIndexPath(indexPath) as YourTableCellClass

if cell.textLabel.text == "myText" {
    // whatever
}