1

I have TableView with multiple rows, I am selecting them and adding them to the Label text when i deselect any of the row I am unable to remove it from Label Text

Here's My Code:

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

Here's What i get as Output:

I first selected 3 rows it got appended to Label.text and after i deselect the row Label.text remains same

Anyone who can help me in this code?

Jayesh Gyanchandani
  • 305
  • 2
  • 6
  • 16

5 Answers5

2

Try this one and its perfectly working for me,

Consider this sample variables

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

add this functionality in your 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;
}
1

Please use an array to do this. Declare NSMutableArray at the top

var arrSelectedTexts:NSMutableArray = [String]()

Now in

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
    }
Poles
  • 3,585
  • 9
  • 43
  • 91
1

you can add a flag - hasSelected in CategoryDB

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

}

Hiroki Murahi
  • 270
  • 1
  • 5
1

First thing remove categoryTable.allowsMultipleSelectionDuringEditing = true from cellForRowAtIndexPath and add it to viewDidload because cellForRowAtIndexPath manyn time when your cell reuse so no need to set it that many time!

Now you should implement didDeselectRowAtIndexPath delegate method also.

In that method you will get indexPath.row for the row that will be deSelect or unselect, from that indexPath.row you can get your object from your array that you need to remove from the string that you are displaying in the label.

So, in didDeselectRowAtIndexPath first convert your label text to mutable array then remove object from that array then convert that array to string again and show on label!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

you just need to implement a delegate method of UITableView like below

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    // update your label here
}
Umair Afzal
  • 4,947
  • 5
  • 25
  • 50