如何禁用除选定单元格之外的所有单元格

时间:2016-08-23 11:57:19

标签: ios swift uitableview

如何禁用与所选cells之外的所有didSelectRowAtIndexPath的互动,然后在再次选择单元格后启用互动?

我在if cell.isExpanded { UIView.animateWithDuration(0.3) { cell.contentView.layoutIfNeeded() self.tableView.userInteractionEnabled = false cell.userInteractionEnabled = true } } 方法中试过这个:

tableView

但这会停用整个cell,包括当前选定的$(document).ready(function() { var mainDataTable = $("#mainDataTable").DataTable({ "pagingType" : "full_numbers", "processing" : true, "serverSide" : true, "jQueryUI" : true, "ajax" : "/JsonData", "columns" : [ { "data" : "caller" }, { "data" : "event" }, { "data" : "receiver" }, { "data" : "timestamp" } ] }); $("#mainDataTable tbody").on("dblclick", "tr", function () { var data = mainDataTable.row().data(); $("#modalDialogBody").html( '<table class="display jqueryAllCallsDataTable" id="allCalls"><thead><tr>' + '<th>Timestamp</th><th>Talk Duration</th><th>Receiver</th><th>Type</th></tr>' + '</thead><tbody><!-- Data will go here --></tbody></table>'); $("#modalDialogTitle").text(data[0] + "#: All Calls"); $("#allCalls").DataTable({ "pagingType": "full_numbers", "processing": true, "serverSide": true, "jQueryUI": true, "ajax": { "url": "/JsonData", "data": function (d) { d.orderByTimestampDesc = true; d.callerId = data[0]; } }, "searching": false, "ordering": false, "columns": [ {"data": "timestamp"}, {"data": "talkDuration"}, {"data": "receiver"}, {"data": "type"} ] }); $("#modalDialog").modal(); }); $("#mainDataTable tbody").on("click", "tr", function() { var data = mainDataTable.row().data(); $("#modalDialogBody").html( '<table class="display jqueryCallDetailsDataTable" id="callDetails"><thead>' + '<tr><th>Caller</th><th>Event</th><th>Receiver</th><th>Timestamp</th></tr>' + '</thead><tbody><!-- Data will go here --></tbody></table>'); $("#modalDialogTitle").text(data[0] + "#: Regular/Cancelled call"); $("#callDetails").DataTable({ "pagingType": "full_numbers", "processing": true, "serverSide": true, "jQueryUI": true, "ajax": { "url": "/JsonData", "data": function (d) { d.callerId = data[0]; } }, "searching": false, "ordering": false, "columns": [ {"data": "caller"}, {"data": "event"}, {"data": "receiver"}, {"data": "timestamp"} ] }); $("#modalDialog").modal(); }); });

2 个答案:

答案 0 :(得分:3)

首先你的tableView需要选择单元格,在选择了一个单元格后,你希望其他单元格不能选择除了选定的单元格,为此你可以创建一个类型为{{1的实例属性并使用它存储在NSIndexPath内,并像这样在didSelectRowAtIndexPath内比较它的值。

cellForRowAtIndexPath

<强>的cellForRowAtIndexPath

var selectedIndexPath: NSIndexPath?

<强> didSelectRowAtIndexPath方法

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell
    if (self.selectedIndexpath != nil) {
        if self.selectedIndexpath == indexPath {
            cell.userInteractionEnabled = true
        }
        else {
            cell.userInteractionEnabled = false
        }
    }
    else {
        cell.userInteractionEnabled = true
    }
    return cell
}

注意:如果选择展开的单元格,我已将单元格func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if self.selectedIndexpath != indexPath { self.selectedIndexpath = indexPath } else { self.selectedIndexpath = nil } tableView.reloadData() } 再次设置为true。

答案 1 :(得分:0)

使用

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

找到所选单元格并存储在变量中选择的indexPath并重新加载tableview。

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

通过检查存储的indexPath来禁用除选择的单元格以外的所有单元格。

希望这有帮助。

相关问题