Can you animate a height change on a UITableViewCell when selected?

前端 未结 21 1021
天涯浪人
天涯浪人 2020-11-22 04:41

I\'m using a UITableView in my iPhone app, and I have a list of people that belong to a group. I would like it so that when the user clicks on a particular pers

相关标签:
21条回答
  • 2020-11-22 04:48

    Swift Version of Simon Lee's answer .

    // MARK: - Variables 
      var isCcBccSelected = false // To toggle Bcc.
    
    
    
        // MARK: UITableViewDelegate
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    
        // Hide the Bcc Text Field , until CC gets focused in didSelectRowAtIndexPath()
        if self.cellTypes[indexPath.row] == CellType.Bcc {
            if (isCcBccSelected) {
                return 44
            } else {
                return 0
            }
        }
    
        return 44.0
    }
    

    Then in didSelectRowAtIndexPath()

      func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
    
        // To Get the Focus of CC, so that we can expand Bcc
        if self.cellTypes[indexPath.row] == CellType.Cc {
    
            if let cell = tableView.cellForRowAtIndexPath(indexPath) as? RecipientTableViewCell {
    
                if cell.tag == 1 {
                    cell.recipientTypeLabel.text = "Cc:"
                    cell.recipientTextField.userInteractionEnabled = true
                    cell.recipientTextField.becomeFirstResponder()
    
                    isCcBccSelected = true
    
                    tableView.beginUpdates()
                    tableView.endUpdates()
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 04:48

    Inputs -

    tableView.beginUpdates() tableView.endUpdates() these functions will not call

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {}

    But, if you do, tableView.reloadRows(at: [selectedIndexPath! as IndexPath], with: .none)

    It will call the func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {} this function.

    0 讨论(0)
  • 2020-11-22 04:48

    Get the indexpath of the row selected. Reload the table. In the heightForRowAtIndexPath method of UITableViewDelegate, set the height of the row selected to a different height and for the others return the normal row height

    0 讨论(0)
  • 2020-11-22 04:50

    Heres a shorter version of Simons answer for Swift 3. Also allows for toggling of the cell's selection

    var cellIsSelected: IndexPath?
    
    
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        cellIsSelected = cellIsSelected == indexPath ? nil : indexPath
        tableView.beginUpdates()
        tableView.endUpdates()
      }
    
    
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if cellIsSelected == indexPath {
          return 250
        }
        return 65
      }
    
    0 讨论(0)
  • 2020-11-22 04:52
    BOOL flag;
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        flag = !flag;
        [tableView beginUpdates];
        [tableView reloadRowsAtIndexPaths:@[indexPath] 
                         withRowAnimation:UITableViewRowAnimationAutomatic];
        [tableView endUpdates];
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES == flag ? 20 : 40;
    }
    
    0 讨论(0)
  • 2020-11-22 04:52

    Check this method after iOS 7 and later.

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return UITableViewAutomaticDimension;
    }
    

    Improvements have been made to this in iOS 8. We can set it as property of the table view itself.

    0 讨论(0)
提交回复
热议问题