如果包含在数组

时间:2015-12-24 16:09:02

标签: 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


}

2 个答案:

答案 0 :(得分:0)

我相信这可能是因为您正在重复使用行

的单元格

let cell:SubjectsTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! 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

} 

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

修改 我刚刚注意到在select和取消选择tableview函数中,实际上并没有更新给定行的selectedSubjects数组。你需要在调用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

}

答案 1 :(得分:0)

var userCLient = string是一个空字符串数组声明..

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedRow:UITableViewCell = tableView.cellForRow(at: indexPath)!

        if selectedRow.accessoryType == UITableViewCellAccessoryType.none{
            selectedRow.accessoryType = UITableViewCellAccessoryType.checkmark
            selectedRow.tintColor = UIColor.green
            var dict = arrRes[(indexPath as NSIndexPath).row]
            let EmpID = dict["user_id"] as? Int
            self.userCLient.append(String(describing: EmpID!))
            print("Employee:\(self.userCLient)")
        }
        else{
            selectedRow.accessoryType = UITableViewCellAccessoryType.none
            var dict = arrRes[(indexPath as NSIndexPath).row]
            let EmpID = dict["user_id"] as? Int
            if let index = self.userCLient.index(of: String(describing: EmpID!)) {
                self.userCLient.remove(at: index)
            }
            print("Employee:\(self.userCLient)")
        }

    }