Rajat Attri
Rajat Attri

Reputation: 23

How to select UITableViewCell using function, without tapping it?

I am populating my tableView using below mentioned array. If user searches an array and item is found then that cell should be selected.

    (
        {
        barcode = 8901058847857;
        image =         (
        );
        name = "Maggi Hot Heads";
        productTotal = "60.00";
        quantity = 3;
    },
        {
        barcode = 8901491101837;
        image =         (
        );
        name = "Lays Classic Salted";
        productTotal = "20.00";
        quantity = 1;
    }
)

I am using below mentioned code to search barcode in array but I don't know how to proceed further, how to select tableViewCell in else case. There is code written in tableViewCell setSelected.

let namePredicate = NSPredicate(format: "barcode contains[c] %@",code)
    let filteredArray = orderDetailsArray.filter { namePredicate.evaluate(with: $0) }
    if filteredArray.isEmpty {
        let alert = UIAlertController(title: "Alert", message: "Item not found in cart.", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:  { action in
            self.scan()
        }))
        self.present(alert, animated: true, completion: nil)
    }else{
        print("item found")
        // Need to do selection here
   }

Upvotes: 1

Views: 77

Answers (3)

KKRocks
KKRocks

Reputation: 8322

else{
        print("item found")
        1. add to that object in separate array

          yourMutableArr.append("scannedString") // which are scanned 
                                                 recently

        2. reload tableview
           [yourTblView reloadData];

        3. In cellForRowAtIndexPath compare that barcodes with datasource array's barcode 

          let strBarcode: String = dict["barcode"][indexPath.row]
             if yourMutableArr.contains(strBarcode) {
              //addNewItem
                      cell.accessoryType = .Checkmark
             }

   }

Upvotes: 1

Rikh
Rikh

Reputation: 4232

There are two ways that i can think of to achieve what you want.

//First
let filteredArray = dataArray.filter{$0["barcode"]!.contains("8901058847857")}

var tempArray = [Int]()

for dict in filteredArray{

   tempArray.append(dataArray.index{$0 == dict}!)
}

//this will have the appropriate rows that you want selected based on your initial and non filtered array   
print(tempArray)

Now all you have to do is try and figure out how to use this in your approach. The tempArray contains the indexes of the rows to select that matches your requirement. Now all you have to do is call the UITableView's selectRowMethod and pass in the indexPath of the rows you have now

If all you want to do is to select the rows based on the condition and you aren't using the filteredArray, then there is no point in adding the elements that match your criteria in another array. You should use the following approach.

//Second approach
var index = 0
var tempArray = [Int]()

for dict in dataArray{

    if (dict["barcode"]?.contains("8901058847857"))!{

        //Or just select the row at this index value
        tempArray.append(index)
    }

    index += 1
}

Upvotes: 1

Saheb Roy
Saheb Roy

Reputation: 5967

If your tableview is populated according to the array in a sequencial manner then, you can do this in the else block : Find the index of the last object of the filtered item in the main item. that should fetch you the indexpath.row of the tableview, which has the cell according to the array DS. You can fetch the cell by cellForRowAtIndexPath by providing the index of the array and getting the cell, then you can turn the selection of the cell and reload the indexpath.

Currently I am not in my mac, hence i couldnt write the code, but I explained the logic in finding it. Hope this helps.

Upvotes: 0

Related Questions