1

我有包含多行的 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 行,它被附加到Label.text,在我取消选择行后,Label.text保持不变

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

4

5 回答 5

2

试试这个,它非常适合我,

考虑这个样本变量

let animals : [String] = ["Dog","Cat","Lion","Tiger","Cow","Buffalo","Horse"]
var sampleArray = [String]()
var samleLabel = UILabel()

在您的 didSelectRowAtIndexPath 中添加此功能

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;
}
于 2016-11-18T07:52:51.177 回答
1

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

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

        categoryList!.hasSelected = true

        refreshLabel()
    }

}

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

        categoryList!.hasSelected = false

        refreshLabel()
    }

}

func refreshLabel(){
     var label = ""
     for object in categoryData as! CategoryDB {
        if(object.hasSelected){
            label += "\(categoryList!.category), "
        }
     }
     categoryLabel.text! = label

}

于 2016-11-17T07:38:33.553 回答
1

请使用数组来执行此操作。在顶部声明 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
    }
于 2016-11-17T07:30:39.127 回答
1

首先要删除categoryTable.allowsMultipleSelectionDuringEditing = truecellForRowAtIndexPath添加它,viewDidload因为cellForRowAtIndexPath您的单元格重复使用的时间很多,所以不需要设置那么多次!

现在你也应该实现didDeselectRowAtIndexPath委托方法。

在该方法中,您将获得indexPath.row将是deSelector的行unselect,从中indexPath.row您可以从数组中获取您需要从标签中显示的字符串中删除的对象。

因此,didDeselectRowAtIndexPath首先转换your label text to mutable array然后从该数组中删除对象,然后将该数组再次转换为字符串并显示在标签上!

于 2016-11-17T07:42:47.980 回答
0

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

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    // update your label here
}
于 2016-11-17T07:27:12.417 回答