2016-04-28 16 views
10

Jestem w stanie rozwijać i zwijać komórki, ale chcę wywoływać funkcje (rozszerzać i zwijać) wewnątrz UITableViewCell, aby zmienić tytuł przycisku.Rozwiń i zwiń komórki widoku tabeli

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

Proszę sprawdzić to rozwiązanie: [wprowadź opis link tutaj] (https://stackoverflow.com/questions/21396907/how-to-programmatically-increase-uitableview-cells-height-in-iphone/45424594# 45424594) –

Odpowiedz

12

Jeśli chcesz komórka dostać fizycznie większa, to gdzie masz swój sklep NSIndexPath, w heightForRow: użytku:

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

Potem, gdy chcesz rozszerzyć jeden w didSelectRow:

storedIndexPath = indexPath 
tableView.beginUpdates 
tableView.endUpdates 

Edit

To powoduje, że komórki animować się coraz większy, nie potrzebują dodatkowych bloków animacji w komórce.

Edycja 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

Jestem w stanie zwinąć i rozwinąć komórkę, tak jak powiedziałeś. Ale chcę zmienić tytuł przycisku w komórce, więc jak mogę wywołać funkcję wewnątrz komórki. –

+0

proszę sprawdzić edit2: – SeanLintern88

+0

Wywoływanie funkcji dwóch komórek. Mam na myśli, kiedy klikam jedną komórkę, dwie komórki rozszerzają biegi funkcji :( –

2

Wszystko czego potrzebujesz to realizować w ten sposób UITableView Delegat:

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

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

Domyślnie estimatedHeight jest CGRectZero, kiedy ustawić jakąś wartość dla niego, umożliwia automatycznej zmiany (taka sama sytuacja jak w przypadku UICollectionView), można to zrobić nawet:

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

Następnie musisz skonfigurować ograniczenia wewnątrz komórki.

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

Nadzieję, że to pomaga)

+0

To powinno być komentarzem –

+0

Użycie UITableViewAutomaticDimension dla averageHeight pokonuje cały cel o użyciu szacowanej wysokości - po pierwsze wywoła ona automatyczny układ dla każdej pozycji indexPath w tabeli, aby obliczyć wysokość całej zawartości, zamiast korzystania z automatycznego układu tylko dla widocznych komórek, a wartość szacunkowej wysokości (która jest o wiele szybsza do obliczenia) dla wszystkich niewidocznych ścieżek indeksowych –

1

W MyTicketsTableViewController klasy, wewnątrz cellForRowAtIndexPath metody źródła danych dodać docelową dla przycisku.

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

Nie rozumiem, do czego służy –

+0

twoja metoda wywoływania rozwijania i zwijania w tableviewcell class.instead tego, w twoim tableviewcontroller deklaruj metodę expand, collapse oraz w wywołaniu cellForRowAtIndexPath taka metoda – Jeyamahesan

Powiązane problemy