Галочка UITableView только для выбранной строки - PullRequest
0 голосов
/ 29 мая 2018

У меня проблема в том, что

Я использую табличное представление в приложении и хочу, чтобы рядом с каждой выбранной строкой создавалась галочка.Но галочка также генерируется в строках других разделов, за исключением строки в разделе, где страница прокручивается вниз.Однако, когда я печатаю ячейку, по которой щелкнули, результат будет правильным.

Это мой код для создания галочки:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath as IndexPath, animated: true)

    let row = indexPath.row
    let indexpath = NSIndexPath(row: indexPath.row, section: indexPath.section)

    let currentCell = tableView.cellForRow(at: indexpath as IndexPath) as! UITableViewCell
    currentCell.backgroundColor = UIColor.gray 

    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark             

    for organ in self.dataSource.organs {
        if(organ.name == organName) {
            for sympton in organ.symptonList {
                if (sympton.name == self.symptonName ){
                    self.symptonList.append(sympton.questionList[indexPath.section].question  + " " + sympton.questionList[indexPath.section].answerList[row].lowercased())
                    print("*******")
                    print(sympton.questionList[indexPath.section].question  + " " + sympton.questionList[indexPath.section].answerList[row].lowercased())
                }
            }                
        }
    }
}

Мой метод cellForRowAt:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath as IndexPath)
    let row = indexPath.row
    for organ in self.dataSource.organs {
        if(organ.name == organName) {
            for sympton in organ.symptonList {
                if(sympton.name == symptonName) {
                    cell.textLabel?.text = sympton.questionList[indexPath.section].answerList[row]
                }
            }
        }
    }
    return cell

}

Это очень полезно для меня, если у вас есть какие-либо предложения.

1 Ответ

0 голосов
/ 29 мая 2018

Сначала добавьте массив IndexPaths в вашем контроллере представления:

var selectedIndexes: [IndexPath] = [IndexPath]()

Затем ваш didSelectRowAtIndexPath:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    if let index = selectedIndexes.index(where: {$0.row == indexPath.row && $0.section == indexPath.section}){
        selectedIndexes.remove(at: index)
    }
    else {
        self.selectedIndexes.append(indexPath)
    }

    for organ in self.dataSource.organs {
        if(organ.name == organName) {
            for sympton in organ.symptonList {
                if (sympton.name == self.symptonName ){
                    self.symptonList.append(sympton.questionList[indexPath.section].question  + " " + sympton.questionList[indexPath.section].answerList[row].lowercased())
                    print("*******")
                    print(sympton.questionList[indexPath.section].question  + " " + sympton.questionList[indexPath.section].answerList[row].lowercased())
                }
            }                
        }
    }
}

Затем в cellForRowAtIndexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath as IndexPath)
    let row = indexPath.row
    for organ in self.dataSource.organs {
        if(organ.name == organName) {
            for sympton in organ.symptonList {
                if(sympton.name == symptonName) {
                    cell.textLabel?.text = sympton.questionList[indexPath.section].answerList[row]
                }
            }
        }
    }
    if(self.selectedIndexes.contains(where: {$0.row == indexPath.row && $0.section == indexPath.section})) {
        cell.backgroundColor = UIColor.gray 
        cell.accessoryType = .checkmark             
    }
    else {
         cell.backgroundColor = UIColor.white
         cell.accessoryType = .none             
    }
    return cell

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...