Bas
Bas

Reputation: 780

Handling touch events to one UITableCell

I have some UITableViewCells in which I can perform click actions.

When a click action starts the cell will expand and a UIPickerView, UITextView or other data will be displayed. (See example images)

When you click the TableCell again the cell will collapse and the original state will be displayed.

Currently every cell expands when I click in a UITableViewCell with an expand action. When I click on a cell I want every other cell to be collapsed and only expand the clicked one.

Question:

How can I give my table cell, which is expanded, a state in which it will receive all touch events. (Become the first responder for the whole screen) and close it first and after the close action send the click event to the corresponding UITableViewCell.

I have made the UITextView first responder and that will close the keyboard after it's poped up, but I want the table cell to be the handler of the click events.

Example code

func togglePicker() {
    //This function is called when the UITableCell is clicked.

    canBecomeFirstResponder()

    // Some code here which adds UIPickerView, UITextView or other data.

    setNeedsLayout()
}

I tried this code, but this cell only receives touch events which are triggered in this cell and not outside its boundaries.

Example images

Orginal cell state

First cell is expanded

Upvotes: 4

Views: 971

Answers (2)

Bas
Bas

Reputation: 780

I implemented the following solution:

class CustomCell: UITableViewCell {

   var delegate: CustomCellDelegate?
   var focussed: Bool = false

   function becomeFocussed() {
      //Put some code here to change the design
      setNeedsLayout()
      focussed = true
      delegate?.isFocussed(self)
   }

   function becomeNormaleState() {
      //Put some code here to change the design to the original state.
      focussed = false
      setNeedsLayout()
   }

}

Every Cell has two functions: becomeFocussed() and becomeNormaleState(). When the cell is clicked the function becomeFocussed() should be called. This functions tells the delegate that the cell is selected.

In the TableViewController the function isFocussed(cell : UITableViewCell), loops through al cells of the current tableView en calls "becomeNormaleState()" for every cells which is focussed.

Upvotes: 1

Shankar BS
Shankar BS

Reputation: 8402

okay, by looking at the images u are changing the frame of the cell, one solution for When I click on a cell I want every other cell to be collapsed and only expand the clicked one. as u asked in the question, u can store the index path of the clicked cell and use this collapse other cell if that cell's picker view is showing for example,

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if showIndexPath?.row == indexPath.row 
    {
        return 330 //expanded height for example
    }
    else {
         return 100 //normal state height for example
    }
}

 //in this method u can decide which one to expand or collapse 
 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    //if user selected the same cell again close it
    if showIndexPath?.row == indexPath.row 
    {
        //already expanded, collapse it 
        showIndexPath = nil
        tableView .reloadRowsAtIndexPaths([showIndexPath!], withRowAnimation: .Fade)

    }
    else
    {
        //expand this cell's index path 
        showIndexPath = indexPath
        tableView .reloadRowsAtIndexPaths([showIndexPath!], withRowAnimation: .Fade)
    }
}

Upvotes: 0

Related Questions