didSelectRowAtIndexPath检查Swift

时间:2015-07-12 10:58:16

标签: ios swift uitableview didselectrowatindexpath

我有一个UiTableView,可以在不同的部分填充数据。

struct ResultRow {
   var name: String
   var dateRanges: [[NSDate]]
   var hours: [Int]
}

var result : [ResultRow] = []
var selectedMarks2 : [ResultRow] = []



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
    tableView.allowsMultipleSelection = true

    cell.textLabel?.text = "row=\(indexPath.row)section=\(indexPath.section) \(result[indexPath.section].name) \(result[indexPath.section].dateRanges[indexPath.row]) \(result[indexPath.section].hours[indexPath.row])"

    cell.textLabel?.font = UIFont.systemFontOfSize(8.0)


    if let selectedPaths = tableView.indexPathsForSelectedRows {
        let selected = selectedPaths.filter(){ $0 == indexPath }
        if selected.count > 0 {
            cell.accessoryType = .Checkmark
        } else {
            cell.accessoryType = .None
        }
    }

    return cell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

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

    if (selectedMarks2.contains(result[indexpath.row])){

        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            cell.accessoryType = .None
        }
        print("erase")
        selectedMarks2.removeObject(result[indexPath.row])

    } else {


        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            cell.accessoryType = .Checkmark
        }
        print("add")
        selectedMarks2.append(result[indexpath.section]) //add the object to selectedMarks

    }
    tableView.reloadData()

}

问题在于,当我尝试在表格的部分添加所选行时,我遇到了问题。如果我选择任何部分的一行,则selectionMarks2变量会附加同一部分的所有行。我只想附加我选择的具体部分的混凝土排。有人可以帮忙吗?

0 个答案:

没有答案