如何根据所选单元格在tableview中设置启用的checkimage [英] How to set enabled checkimage in tableview based on the selected cells

查看:104
本文介绍了如何根据所选单元格在tableview中设置启用的checkimage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器获取以前选择的类别列表。比如说我从服务器获取的example.cateogrylist是在下面的格式中

I am fetching previously selected categorylist from the server. say for an example.cateogrylist i fetched from the server was in following formate

categoryid : 2,6,12,17

现在我需要做的是想在我的tableview中启用基于此类别列表的复选标记,为此目的我将此列表转换为[Int]数组,如下所示:

now what i need to do is want to enable checkmark in my tableview based on this categorylist,for that purpose i converted this list into an [Int] array like this :

func get_numbers(stringtext:String) -> [Int] {
    let StringRecordedArr = stringtext.components(separatedBy: ",")
    return StringRecordedArr.map { Int($0)!}
}

在viewDidLoad()中:

in viewDidLoad() :

  selectedCells = self.get_numbers(stringtext: UpdateMedicalReportDetailsViewController.catId)
  print(myselection)

打印时它给我这样的结果: [12,17,6,8,10]

while printing it's giving me results like this : [12,17,6,8,10]

我想基于这个数组启用checkimage。我尝试了一些代码,同时打印它给我正确的结果,就像我在发布时选择的任何类别,我能够获取它但未能将此选择放回tableview.Requirement:虽然我打开这个页面它应该显示我根据从服务器获取的类别列表进行选择。

i want to enable checkimage based on this array.I tried some code while printing its giving me the right result like whatever the categories i selected at the time of posting ,i am able to fetch it but failed to place back this selection in tableview.Requirement : while i open this page it should show me the selection based on the categorylist i fetched from the server.

var selectedCells : [Int] = []

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell1  = table.dequeueReusableCell(withIdentifier: "mycell") as! catcell

    cell1.mytext.text = categoriesName[indexPath.row] 
      if UpdateMedicalReportDetailsViewController.flag == 1
    {
          selectedCells = self.get_numbers(stringtext: UpdateMedicalReportDetailsViewController.catId)
        cell1.checkimage.image = another

        print(selectedCells)
    }
    else
    {

        selectedCells = []
        cell1.checkimage.image = myimage

    }
    return cell1

}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = table.cellForRow(at: indexPath) as! catcell
    cell.checkimage.image = myimage
    if cell.isSelected == true
    {
        self.selectedCells.append(indexPath.row)
        cell.checkimage.image = another
    }
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = table.cellForRow(at: indexPath) as! catcell
    if cell.isSelected == false
    {

        self.selectedCells.remove(at: self.selectedCells.index(of: indexPath.row)!)

        cell.checkimage.image = myimage

    }
}

输出:

推荐答案

这是大多数应用程序中非常常见的用例。我假设你有一个所有类别的数组,然后是一系列选定的类别。您需要做的是在 cellForRowAtIndexPath 中,检查所有类别数组中当前索引路径行的相应类别是否也出现在所选类别数组中。您可以通过比较ID等来实现此目的。

This is a very common use case in most apps. I'm assuming you have an array of all categories, and then an array of selected categories. What you need to do is in cellForRowAtIndexPath, check to see if the current index path row's corresponding category in the "all categories" array is also present in the "selected categories" array. You can do this by comparing id's etc.

如果您有匹配,那么您就知道需要选择/检查该单元格。一个干净的方法是给你的单元子类一个自定义加载方法,你可以传递一个标记选择/检查。

If you have a match, then you know that the cell needs to be selected/checked. A clean way to do this is give your cell subclass a custom load method and you can pass a flag for selected/checked.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = table.dequeueReusableCell(withIdentifier: "mycell") as! catcell

let category = self.categories[indexPath.row] // Let's say category is a string "hello"
Bool selected = self.selectedCategories.contains(category)

cell.load(category, selected)

return cell

}

所以使用上面的代码,假设 categories 只是一个类别字符串数组,如 hello world stackoverflow 。我们检查 selectedCategories 数组是否包含当前单元格/行的类别字。

So with the code above, let's say that categories is just an array of category strings like hello, world, and stackoverflow. We check to see if the selectedCategories array contains the current cell/row's category word.

假设我们设置的单元格类别为 hello ,并且 selectedCategories 确实包含它。这意味着选中的 bool设置为true。

Let's say that the cell we're setting up has a category of hello, and selectedCategories does contain it. That means the selected bool gets set to true.

然后我们传递类别选择值进入单元子类'load方法,在该load方法中,您可以将单元格的标题文本设置为类别,你可以检查选择是真还是假,如果是,你可以显示选中的方框UI。

We then pass the category and selected values into the cell subclass' load method, and inside that load method you can set the cell's title text to the category and you can check if selected is true or false and if it's true you can display the checked box UI.

这篇关于如何根据所选单元格在tableview中设置启用的checkimage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆