swift - 如果包含在数组中,则将 UITableView 行设置为选中

标签 swift uitableview

我将数据存储在领域数据库中。我从数据库中检索信息并将其放在一个数组中。然后我搜索数组并将单元格设置为选中(如果它包含在数组中)。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {

    let cell:SubjectsTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! SubjectsTableViewCell

    var subject: Subjects!

    subject = Subjects[Int(indexPath.row)]

    cell.subject.text = subject.subjectTitle

    if chosenSubjects.contains(subject.subjectTitle) {

       cell.selected = true
       cell.accessoryType = .Checkmark
       tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
        cell.highlighted = true

    } else  {

        tableView.deselectRowAtIndexPath(indexPath, animated: false)
    }


    return cell

}

不幸的是,上面的代码给了我一些奇怪的结果。有时单元格会突出显示并显示复选标记,有时表格中的第一个单元格未突出显示但显示复选标记。有时我必须按单元格两次才能取消选择。请注意,我还设置了 didSelectRowAtIndexPath 和 didDeselectRowAtIndexPath 如下:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // Get the row data for the selected row
    self.tableView.allowsMultipleSelection = true
    let cell:SubjectsTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! SubjectsTableViewCell
    cell.accessoryType = .Checkmark


}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    // Get the row data for the selected row
    self.tableView.allowsMultipleSelection = true
     let cell:SubjectsTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! SubjectsTableViewCell
    cell.accessoryType = .None


}

最佳答案

我相信这可能会发生,因为您在您的生产线中重复使用细胞

让 cell:SubjectsTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") 作为! SubjectsTableViewCell

相反,尝试做这样的事情:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {

    let cell:SubjectsTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! SubjectsTableViewCell

    var subject: Subjects!

    subject = Subjects[Int(indexPath.row)]

    cell.subject.text = subject.subjectTitle

    if chosenSubjects.contains(subject.subjectTitle) {

       cell.selected = true
       cell.accessoryType = .Checkmark
       tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
       cell.highlighted = true

    } else  {

       cell.selected = false
       cell.accessoryType = .None
       cell.highlighted = false

       tableView.deselectRowAtIndexPath(indexPath, animated: false)
    }


    return cell

} 

希望这对您有所帮助;如果没有,请告诉我!


编辑: 我只是注意到在您的选择和取消选择 tableview 函数中,您实际上并没有更新给定行的 chosenSubjects 数组。您需要在调用 cellForRowAtIndexPath 之前更新它;否则,if 条件(在您的 cellForRowAtIndexPath 中)将失败 - 从而导致奇怪的行为!

试试这个:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // Get the row data for the selected row
    self.tableView.allowsMultipleSelection = true

    var subject: Subjects! = Subjects[Int(indexPath.row)]
    chosenSubjects.addObject(subject.subjectTitle)

    let cell:SubjectsTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! SubjectsTableViewCell
    cell.accessoryType = .Checkmark


}

还有这个:

 func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    // Get the row data for the selected row
    self.tableView.allowsMultipleSelection = true

    var subject: Subjects! = Subjects[Int(indexPath.row)]
    chosenSubjects = chosenSubjects.filter() { $0 != subject.subjectTitle }

     let cell:SubjectsTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! SubjectsTableViewCell
    cell.accessoryType = .None

关于swift - 如果包含在数组中,则将 UITableView 行设置为选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455300/

相关文章:

ios - 确保两个计时器的执行逻辑不会同时运行

nsstring - NSRange 到 Range<String.Index>

ios - 表格 View 、静态单元格、更改单元格高度

ios - 关闭 B View 时不会调用 UITableView 函数

ios - ViewwithTag 不获取单元格标签

objective-c - 存档的应用程序不工作。从 XCODE 运行成功

快速获取用户输入并提示

ios - 注册我不使用的单元格对性能有很大影响吗?

ios - UITableView 在顶部有差距

iPhone:获取结果 Controller ,不要使用 UITableView 中的部分