ios - 如何使用函数选择UITableViewCell而不点击它?

标签 ios xcode uitableview swift3

我正在使用下面提到的数组填充tableView。如果用户搜索数组并找到项目,则应选择该单元格。

    (
        {
        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
   }

最佳答案

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
             }

   }

关于ios - 如何使用函数选择UITableViewCell而不点击它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43488878/

相关文章:

objective-c - 具有多个 CDVViewController 的 Phonegap/Cordova

ios - 当从 iOS 中的另一个 Storyboard场景调用 viewController 时, TableView 变得空白(加载或重建)

ios - 将 UIPickerView 选择保存到 NSUserDefaults swift

ios - 无法将类型 'Response<AnyObject, NSError>' 的值转换为闭包结果类型 'NSDictionary'

ios - 如何在 OSX 10.10 Yosemite 上安装 xcode 5

ios - 代码中的 XCode 基础 SDK 版本

swift - UITableView 只显示一节

ios - 在 Swift 3.0 中将 UIButton 分层到 UILabel 上是否合适?

ios - NSAttributedString 中的 HTML 呈现速度非常慢

ios - 从 Objective C 中的函数返回函数?