2016-04-28 11 views
10

Sono in grado di espandere e comprimere celle, ma voglio chiamare funzioni (espandere e comprimere) all'interno di UITableViewCell per cambiare titolo pulsante.Espandi e comprimi celle tableview

Iphone 5

 
import UIKit 

class MyTicketsTableViewController: UITableViewController { 

    var selectedIndexPath: NSIndexPath? 
    var extraHeight: CGFloat = 100 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 5 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTicketsTableViewCell 
     return cell 
    } 

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     if(selectedIndexPath != nil && indexPath.compare(selectedIndexPath!) == NSComparisonResult.OrderedSame) { 
      return 230 + extraHeight 
     } 

     return 230.0 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if(selectedIndexPath == indexPath) { 
      selectedIndexPath = nil 
     } else { 
      selectedIndexPath = indexPath 
     } 

     tableView.beginUpdates() 
     tableView.endUpdates() 
    } 
} 

 
import UIKit 

class MyTicketsTableViewCell: UITableViewCell { 

    @IBOutlet weak var expandButton: ExpandButton! 
    @IBOutlet weak var detailsHeightConstraint: NSLayoutConstraint! 

    var defaultHeight: CGFloat! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     defaultHeight = detailsHeightConstraint.constant 

     expandButton.button.setTitle("TAP FOR DETAILS", forState: .Normal) 
     detailsHeightConstraint.constant = 30 
    } 

    func expand() { 
     UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveLinear, animations: { 
      self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.99)) 
      self.detailsHeightConstraint.constant = self.defaultHeight 
      self.layoutIfNeeded() 

      }, completion: { finished in 
       self.expandButton.button.setTitle("CLOSE", forState: .Normal) 
     }) 
    } 

    func collapse() { 
     UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveLinear, animations: { 
      self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.0)) 
      self.detailsHeightConstraint.constant = CGFloat(30.0) 
      self.layoutIfNeeded() 

      }, completion: { finished in 
       self.expandButton.button.setTitle("TAP FOR DETAILS", forState: .Normal) 
     }) 
    } 

} 

+0

Si prega di verificare questa soluzione: [inserire descrizione collegamento qui] (https://stackoverflow.com/questions/21396907/how-to-programmatically-increase-uitableview-cells-height-in-iphone/45424594# 45424594) –

risposta

12

Se si desidera che il cellulare per ottenere fisicamente più grande, allora dove avete il vostro negozio NSIndexPath, in heightForRow: uso:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if selectedIndexPath == indexPath { 
     return 230 + extraHeight 
    } 
    return 230.0 
} 

Poi, quando si desidera espandere uno nella didSelectRow:

storedIndexPath = indexPath 
tableView.beginUpdates 
tableView.endUpdates 

Modifica

Questo renderà le cellule animano stessi sempre più grande, non avete bisogno di blocchi di animazione supplementari in cella.

Edit 2

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if(selectedIndexPath == indexPath) { 
      selectedIndexPath = nil 

      if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTicketsTableViewCell { 
      cell.collapse() 
      } 
      if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow:indexPath.row+1, section: indexPath.section) as? MyTicketsTableViewCell { 
      cell.collapse() 
      } 
     } else { 
      selectedIndexPath = indexPath 

      if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTicketsTableViewCell { 
       cell.expand() 
      } 

      if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow:indexPath.row+1, section: indexPath.section) as? MyTicketsTableViewCell { 
      cell.expand() 
      } 
     } 

     tableView.beginUpdates() 
     tableView.endUpdates() 
    } 
+0

Sono in grado di comprimere ed espandere la cella come hai detto. Ma voglio cambiare il titolo del pulsante all'interno della cella, quindi come posso chiamare una funzione all'interno della cella. –

+0

si prega di controllare edit2: – SeanLintern88

+0

Chiamate di due celle. Voglio dire quando faccio clic su una cella, due celle espanderanno le funzioni :( –

2

Tutto ciò che serve è implementare UITableView delegato in questo modo:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

Per impostazione predefinita, estimatedHeight è CGRectZero, quando si imposta un valore per esso, permette il ridimensionamento automatico (la stessa situazione con UICollectionView), è possibile fare anche così:

tableView?.estimatedRowHeight = CGSizeMake(50.f, 50.f); 

Quindi è necessario impostare i vincoli all'interno della cella.

Controllare questo post: https://www.hackingwithswift.com/read/32/2/automatically-resizing-uitableviewcells-with-dynamic-type-and-ns

Speranza che aiuta)

+0

Questo dovrebbe essere un commento –

+0

Uso di UITableViewAutomaticDimension per stimaHeight sconfitte l'intero scopo di utilizzare HeightHeight in primo luogo - chiamerà il layout automatico su ogni IndexPath nel TableView per calcolare l'altezza per l'intero contenuto, invece di utilizzare il layout automatico solo per le celle visibili e stimatoHeight (che idealmente è molto più veloce per calcolare tutti gli indici non visibili. –

1

In MyTicketsTableViewController classe, all'interno cellForRowAtIndexPath metodo origine dati aggiungere bersaglio per il pulsante.

cell.expandButton.addTarget(self, action: "expandorcollapsed:", forControlEvents: UIControlEvents.TouchUpInside) 
+0

Non capisco a cosa serve? –

+0

i metodi di espansione e compressione della chiamata in tableviewcell class.instanza di ciò, nel proprio tableviewcontroller dichiarare il metodo expand, collapse e nella chiamata cellForRowAtIndexPath il metodo come questo – Jeyamahesan