2017-04-19 4 views
1

私は、以下の配列を使用して自分のtableViewを作成しています。ユーザーが配列を検索し、項目が見つかった場合、そのセルを選択する必要があります。私は以下の使用しています関数を使用してUITableViewCellを選択するには、タップする必要はありませんか?

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

は、配列内のバーコードを検索するためのコードを述べたが、私は他のケースでtableViewCellを選択する方法、さらに進行するのか分かりません。 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 
    } 
+0

あなたは、単にあなたがウルマックを持っていたときにバーコードを濾過し、cellForRow –

答えて

1
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 
      } 

    } 
0

あなたのテーブルビューは、その後、連番の方法で、配列に応じて移入された場合は、他のブロックでこれを行うことができます。メインアイテムでフィルタリング項目の最後のオブジェクトのインデックスを検索します。配列DSに従ってセルを持つtableviewのindexpath.rowを取得する必要があります。 cellForRowAtIndexPathでセルをフェッチするには、配列のインデックスを指定してセルを取得してから、セルの選択を反転してindexpathを再ロードします。

現在私は私のMacにはいないので、私はコードを書くことができませんでしたが、私はそれを見つける際に論理を説明しました。お役に立てれば。

+0

にその配列を比較含む文字列の配列は、uはコードを送信することができますすることができますか? –

1

私はあなたが望むものを達成するために考えることができる2つの方法があります。

//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) 

ここで、このアプローチをどのように使用するかを試してみるだけです。 tempArrayには、要件に一致する選択する行のインデックスが含まれています。今、あなたがしなければならないすべてはUITableViewさんselectRowMethodを呼び出し、あなたがしたいすべての条件に基づいて行を選択することで、あなたが使用していない場合は、あなたが今

を持つ行のindexPathに渡していますfilteredArrayの場合、条件に一致する要素を別の配列に追加することはできません。次の方法を使用する必要があります。

//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 
} 
関連する問題