Ios 选择并取消选择UITableView行

Ios 选择并取消选择UITableView行,ios,uitableview,swift2,multipleselection,uitableviewrowaction,Ios,Uitableview,Swift2,Multipleselection,Uitableviewrowaction,我有多行的TableView,我正在选择它们并将它们添加到标签文本中,当我取消选择任何一行时,我无法将其从标签文本中删除 这是我的密码: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if tableView == categoryTable { let cell = self

我有多行的TableView,我正在选择它们并将它们添加到标签文本中,当我取消选择任何一行时,我无法将其从标签文本中删除

这是我的密码:

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

        if tableView == categoryTable
        {
        let cell = self.categoryTable.dequeueReusableCellWithIdentifier("categoryCell") as! InquiryCategoryTableViewCell

        let Category:CategoryDB = categoryData.objectAtIndex(indexPath.row) as! CategoryDB
        print("Category = \(Category.category)")

            cell.categoryName.text = "\(Category.category)"
            cell.tintColor = color.UIColorFromRGB(0xCEEBFF)
            categoryTable.allowsMultipleSelectionDuringEditing = true
            categoryTable.setEditing(true, animated: false)



        return cell
        }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if tableView == categoryTable
        {
            categoryList = categoryData[indexPath.row] as! CategoryDB

            self.category_id = categoryList!.catg_id



            print("Name = \(categoryList!.category)")
            print("ID = \(self.category_id)")
            categoryLabel.text! += "\(categoryList!.category), "
        }
}
以下是我得到的输出:

我首先选择了它附加到标签上的3行。文本,然后取消选择标签上的行。文本保持不变


有谁能在这段代码中帮助我吗?

您只需要实现
UITableView
的委托方法,如下所示

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    // update your label here
}

您只需要实现
UITableView
的委托方法,如下所示

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    // update your label here
}

请使用数组来执行此操作。在顶部声明NSMutableArray

var arrSelectedTexts:NSMutableArray = [String]()
现在在

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if(!arrSelectedTexts.containsObject(categoryList!.category))
            arrSelectedTexts.addObject(categoryList!.category)

        //Now update your label using the objects of the array

}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        if(arrSelectedTexts.containsObject(categoryList!.category))
           arrSelectedTexts.removeObject(categoryList!.category)

        //Now update your label using the objects of the array
    }

请使用数组来执行此操作。在顶部声明NSMutableArray

var arrSelectedTexts:NSMutableArray = [String]()
现在在

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if(!arrSelectedTexts.containsObject(categoryList!.category))
            arrSelectedTexts.addObject(categoryList!.category)

        //Now update your label using the objects of the array

}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        if(arrSelectedTexts.containsObject(categoryList!.category))
           arrSelectedTexts.removeObject(categoryList!.category)

        //Now update your label using the objects of the array
    }

您可以在CategoryDB中添加一个标志-hasSelected

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
    {
        categoryList = categoryData[indexPath.row] as! CategoryDB

        categoryList!.hasSelected = true

        refreshLabel()
    }
}

}


}

您可以在CategoryDB中添加一个标志-hasSelected

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
    {
        categoryList = categoryData[indexPath.row] as! CategoryDB

        categoryList!.hasSelected = true

        refreshLabel()
    }
}

}


}首先要从
cellforrowatinexpath
中删除
categoryTable.allowsMultipleSelectionDuringEdit=true
,并将其添加到
viewDidload
中,因为
cellforrowatinexpath
在您的单元格重新使用时需要很多时间,所以无需设置那么多时间

现在,您还应该实现
diddescrowatindexpath
delegate方法

在该方法中,您将从
indexPath.row
中获得
indexPath.row
,该行将被
取消选择
取消选择
,从该
indexPath.row
中,您可以从数组中获取需要从标签中显示的字符串中移除的对象


因此,在
中,首先将
标签文本转换为可变数组
,然后从该数组中删除对象,然后再次将该数组转换为字符串并显示在标签上

首先要从
cellforrowatinexpath
中删除
categoryTable.allowsMultipleSelectionDuringEditing=true
并将其添加到
viewDidload
中,因为
cellforrowatinexpath
在您的单元格重新使用时需要很多时间,所以无需设置那么多时间

现在,您还应该实现
diddescrowatindexpath
delegate方法

在该方法中,您将从
indexPath.row
中获得
indexPath.row
,该行将被
取消选择
取消选择
,从该
indexPath.row
中,您可以从数组中获取需要从标签中显示的字符串中移除的对象


因此,在
中,首先将
标签文本转换为可变数组
,然后从该数组中删除对象,然后再次将该数组转换为字符串并显示在标签上

试试这个,它对我非常有效

考虑这个示例变量

let animals : [String] = ["Dog","Cat","Lion","Tiger","Cow","Buffalo","Horse"]
var sampleArray = [String]()
var samleLabel = UILabel()
在DidSelectRowatineXpath中添加此功能

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{ 
    let cell = tableView.cellForRowAtIndexPath(indexPath)!
    cell.textLabel!.text = animals [indexPath.row]

    if sampleArray.contains((cell.textLabel?.text)!)
    {
        sampleArray.removeAtIndex(indexPath.row)
        print(sampleArray)
        samleLabel.text = test().componentsJoinedByString(",")
    }
    else
    {
        sampleArray.append((cell.textLabel?.text)!)
         print(sampleArray)
        samleLabel.text = test().componentsJoinedByString(",")
    }  
}

func test()-> NSArray
{
    print(sampleArray)
    return sampleArray;
}

试试这个,它对我非常有效

考虑这个示例变量

let animals : [String] = ["Dog","Cat","Lion","Tiger","Cow","Buffalo","Horse"]
var sampleArray = [String]()
var samleLabel = UILabel()
在DidSelectRowatineXpath中添加此功能

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{ 
    let cell = tableView.cellForRowAtIndexPath(indexPath)!
    cell.textLabel!.text = animals [indexPath.row]

    if sampleArray.contains((cell.textLabel?.text)!)
    {
        sampleArray.removeAtIndex(indexPath.row)
        print(sampleArray)
        samleLabel.text = test().componentsJoinedByString(",")
    }
    else
    {
        sampleArray.append((cell.textLabel?.text)!)
         print(sampleArray)
        samleLabel.text = test().componentsJoinedByString(",")
    }  
}

func test()-> NSArray
{
    print(sampleArray)
    return sampleArray;
}

要在取消选择行时更新标签吗?@UmairAfzal Yupso调用DIDDENCEROWATINDEXPATH,就像您使用的一样didSelectRowAtIndexPath@UmairAfzal让我试试看,取消选择行时是否要更新标签?@UmairAfzal Yupso调用DIDDENCEROWATINDEXPATH,就像您使用的一样didSelectRowAtIndexPath@UmairAfzal让我试试这个