在表视图中点击单元格不会执行任何操作

时间:2014-09-21 21:41:43

标签: ios uitableview swift

我在我的应用中的一个表视图中遇到选择问题。基本上,问题是我只能通过点击搜索结果的单元格来转换到另一个视图控制器。出于某种原因,我无法在标准表视图中点击单元格。

当我正常点击一个细胞时,没有任何反应。但是,如果我使用搜索栏搜索单元格,我可以点击单元格,它们将按预期将其转换为另一个视图控制器。我无法弄清楚原因。

这是我的代码:

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.

    if tableView == self.searchDisplayController?.searchResultsTableView {
        return searchResults.count
    } else {
    if names.count == 0 {
        if enterButtonTapped == false {
            backgroundLabel.text = "Before you add any transactions, you must first set a budget. You can do this by tapping the 'Budget' tab."
        } else {
            backgroundLabel.text = "You haven't added any transactions yet. Tap the add button to add a new transaction."
        }
        backgroundLabel.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
        backgroundLabel.numberOfLines = 0
        backgroundLabel.textAlignment = NSTextAlignment.Center
        backgroundLabel.sizeToFit()
        backgroundLabel.hidden = false
        backgroundLabel.textColor = UIColor.lightGrayColor()
        clearButton.enabled = false

        self.tableView.backgroundView = backgroundLabel
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        self.tableView.scrollEnabled = false
        return 0
    } else {
        backgroundLabel.hidden = true
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
        self.tableView.scrollEnabled = true
        clearButton.enabled = true
        return names.count
    }
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:CustomTransactionTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
    cell.selectionStyle = UITableViewCellSelectionStyle.None

    if tableView == self.searchDisplayController?.searchResultsTableView {
        cell.paymentNameLabel.text = (searchResults.objectAtIndex(indexPath.row)) as? String
        //println(searchResults.objectAtIndex(indexPath.row))
        var indexValue = names.indexOfObject(searchResults.objectAtIndex(indexPath.row))
        cell.costLabel.text = (values.objectAtIndex(indexValue)) as? String
        cell.dateLabel.text = (dates.objectAtIndex(indexValue)) as? String

        if images.objectAtIndex(indexValue) as NSObject == 0 {
            cell.paymentArrowImage.hidden = false
            cell.creditArrowImage.hidden = true
        } else if images.objectAtIndex(indexValue) as NSObject == 1 {
            cell.creditArrowImage.hidden = false
            cell.paymentArrowImage.hidden = true
        }
        //This works, unless there are two payments with the same name. To revert to previous version, remove tableView if statement and just keep code below the else.
    } else {
        cell.paymentNameLabel.text = (names.objectAtIndex(indexPath.row)) as? String
        cell.costLabel.text = (values.objectAtIndex(indexPath.row)) as? String
        cell.dateLabel.text = (dates.objectAtIndex(indexPath.row)) as? String

        if images.objectAtIndex(indexPath.row) as NSObject == 0 {
            cell.paymentArrowImage.hidden = false
            cell.creditArrowImage.hidden = true
        } else if images.objectAtIndex(indexPath.row) as NSObject == 1 {
            cell.creditArrowImage.hidden = false
            cell.paymentArrowImage.hidden = true
        }
    }
    return cell
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    if tableView == self.searchDisplayController?.searchResultsTableView {
        return false
    } else {
        return true
    }
}

视图控制器通过推送segue连接到故事板中的单元格。

最初我想知道是否是这条线(但事实并非如此):

cell.selectionStyle = UITableViewCellSelectionStyle.None

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

实际上这个问题相当简单。我只需将表格视图属性中的选择设置从“无选择”更改为“单选”。