【问题标题】:How to select UITableViewCell using function, without tapping it?如何使用函数选择 UITableViewCell,而不点击它?
【发布时间】:2017-04-19 07:11:15
【问题描述】:

我正在使用下面提到的数组填充我的 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
   }

【问题讨论】:

  • 您可以简单地制作包含过滤条形码的字符串数组并在 cellForRow 中比较该数组

标签: ios xcode uitableview swift3


【解决方案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
             }

   }

【讨论】:

    【解决方案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 包含要选择的符合您要求的行的索引。现在您所要做的就是调用UITableViewselectRowMethod 并传入您现在拥有的行的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
    }
    

    【讨论】:

      【解决方案3】:

      如果你的 tableview 是按照数组顺序填充的,那么你可以在 else 块中执行此操作:在主项中查找过滤项的最后一个对象的索引。这应该为您获取 tableview 的 indexpath.row,它具有根据数组 DS 的单元格。您可以通过cellForRowAtIndexPath 获取单元格,通过提供数组的索引并获取单元格,然后您可以转动单元格的选择并重新加载索引路径。

      目前我不在我的mac中,因此我无法编写代码,但我解释了找到它的逻辑。希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 2014-12-11
        • 2022-08-16
        • 2021-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多