2014-12-12 11 views
8

Jestem całkiem nowy, aby szybko opracować język i iOS jako całość, więc proszę wybacz mi brak mojej podstawowej wiedzy. Wcześniej próbowałem i powodzeniem wdrożony wiele skrawki sekcje niestandardowe UITableView poprzez plik XIB tworząc TableViewCell a następnie załadowanie go do mojego głównego ViewController i odsyłając go jako zakodowane poniżej:Prawidłowo powracający UIView w viewForHeaderInSection iOS 8 swift

var customView = NSBundle.mainBundle().loadNibNamed("CustomHeader",owner: self, options: nil)[0] as? UIView 
return customView 

Ale odkąd zaczęła się „nie ścieżkę indeksu komórka tabeli ponownego użycia”poszedłem z powrotem do deski kreślarskiej i starał się robić rzeczy programowo tworząc UIView i odesłać go do tej pory jestem okazały się bezskuteczne jednak to co mam zakodowane:

func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!{ 
    if(section == 0) { 
     var view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50)) 
     var label = UILabel(frame: CGRectMake(0,0, tableView.frame.size.width/2, 20)) 
     label.text="My Details" 
     let button = UIButton.buttonWithType(UIButtonType.System) as UIButton 
     button.frame = CGRectMake(0, 0, tableView.frame.size.width/2, 20) 
     button.addTarget(self, action: "visibleRow", forControlEvents:.TouchUpInside) 

     label.setTranslatesAutoresizingMaskIntoConstraints(false) 
     button.setTranslatesAutoresizingMaskIntoConstraints(false) 
     let views = ["label": label,"button":button,"view": view]  
     var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label(20)]-60-[button(20)]-10-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) 
     view.addConstraints(horizontallayoutContraints) 

     return view 
    } 
... 

Jak widać mam Próbuję stworzyć układ, w którym chcę umieścić etykietę i przycisk poziomo, ale w jakiś sposób l ogic nie działa, próbowałem wyłączyć automatyczne ograniczanie ograniczeń w widoku, ale to też nie zadziałało. Proszę pomóż!

Odpowiedz

19

Nigdy nie dodano etykiety ani przycisku, aby "wyświetlić". Również twoje rozmiary nie mają sensu - ustawiasz szerokość etykiety i przycisku na 1/2 szerokości widoku tabeli, ale w twoich ograniczeniach masz 80 punktów wartości spacji, więc nie możesz praca. W żadnym wypadku NIE powinieneś ustawiać żadnych ramek podczas korzystania z automatycznego układu. Pozbądź się tych elementów i dodaj ograniczenie pionowe do etykiety lub przycisku, a także opcję układu, aby wyrównać je pionowo. Ponadto musisz dodać etykietę i przycisk do widoku (i zrobić to przed dodaniem ograniczeń). Coś takiego powinno zadziałać,

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    if(section == 0) { 
      var view = UIView() // The width will be the same as the cell, and the height should be set in tableView:heightForRowAtIndexPath: 
      var label = UILabel() 
      label.text="My Details" 
      let button = UIButton.buttonWithType(UIButtonType.System) as UIButton 
      button.addTarget(self, action: "visibleRow:", forControlEvents:.TouchUpInside) 
      label.setTranslatesAutoresizingMaskIntoConstraints(false) 
      button.setTranslatesAutoresizingMaskIntoConstraints(false) 
      button.setTitle("Test Title", forState: .Normal) 
      let views = ["label": label,"button":button,"view": view] 
      view.addSubview(label) 
      view.addSubview(button) 
      var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label]-60-[button]-10-|", options: .AlignAllCenterY, metrics: nil, views: views) 
      view.addConstraints(horizontallayoutContraints) 

      var verticalLayoutContraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0) 
      view.addConstraint(verticalLayoutContraint) 
      return view 
     } 
     return nil 
    } 


func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 50 
    } 
3
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 20 
} 

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let view = UIView() 
    view.backgroundColor = UIColor.clearColor() 
    return view 
} 
Powiązane problemy