2016-08-09 5 views
1

iOS 7.1.2では、プログラムでUITableViewのセルの高さをプログラム的に調整しようとしています(悲しいことに、それが求められました)。 7以降のすべてのiOSバージョンには問題はありませんが、高さが1のUITableViewディスプレイです。iOS 7.1.2のUITableViewセルの高さ

ここは私のコードです(私は文字通りXcodeのデザインインターフェイスを嫌い、 AutoLayoutを使用します)。 アイデア

Screenshot missed UITableView

import UIKit 

class ChemieListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate { 

private var imageView = UIImageView() 
private var bg_image = UIImage() 
private var titre = UILabel() 
private var button_Back = UIButton() 
var navigationBar = UIView() 
var items:[String] = ["1","A","B","C","D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] 

var lbl_suche_by_txt = UILabel() 
var text_search = UITextField() 
var alph_list = UITableView() 
var lbl_such_by_alph = UILabel() 
var scroll: UIScrollView! 
var scroll_h: CGFloat = CGFloat(0) 
private var waitView = UIView() 
private var loading_view = UIImageView() 
private var loading_text = UILabel() 
private var has_started = false 
private var bg_infro = UIView() 
private var txt_info = UILabel() 
private var options_pics = UIImageView() 



override func viewDidLoad() { 
    super.viewDidLoad() 

    //---------------------Scroll View----------------------------- 
    self.scroll = UIScrollView() 
    scroll_h = 0 
    //----------------------Wait screen--------------------------- 
    waitView.backgroundColor = UIColor.blackColor() 
    waitView.alpha = 0.8 
    let ScreenSize: CGRect = UIScreen.mainScreen().bounds 
    loading_text.text = NSLocalizedString("wait", comment: " ") 
    loading_text.textColor = UIColor(white: 1, alpha: 1) 
    loading_text.textAlignment = .Center 
    loading_text.backgroundColor = UIColor(white: 1, alpha: 0) 

    //----------------------Navigation Bar------------------------- 
    navigationBar.backgroundColor = GetColorFromHex(0x2139D2) 
    //----------------------Back Button---------------------------- 
    var back_img: UIImage? = UIImage(named: "back") 
    if (back_img == nil){ 
     back_img = UIImage(named: "back.png") 
    } 
    button_Back.translatesAutoresizingMaskIntoConstraints = false 
    button_Back.addTarget(self, action: #selector(ProduktViewController.button_back_Pressed), forControlEvents: .TouchDown) 
    button_Back.setBackgroundImage(back_img, forState: UIControlState.Normal) 
    button_Back.contentMode = UIViewContentMode.ScaleAspectFit 
    self.view.addSubview(button_Back) 
    //----------------------Background Image----------------------- 
    //Background Image 
    var BGImage: UIImage? = UIImage(named: "background") 
    if(BGImage == nil){ 
     BGImage = UIImage(named: "background.jpg") 
    } 
    bg_image = BGImage! 
    imageView = UIImageView(frame: self.view.bounds) 
    imageView.image = bg_image 
    imageView.clipsToBounds = true 
    self.view.addSubview(imageView) 
    self.view.sendSubviewToBack(imageView) 
    //------------------------Title------------------------------------ 
    let largeur_title = ScreenSize.width - button_Back.frame.width - 30 
    titre.adjustsFontSizeToFitWidth = true 
    titre.text = NSLocalizedString("liste", comment: " ") 
    titre.textColor = UIColor.whiteColor() 
    titre.frame = CGRect (x: button_Back.frame.origin.x + button_Back.frame.width + 10, y: button_Back.frame.origin.y, width: largeur_title, height: 50) 
    titre.textAlignment = .Center 
    //---------------------txt For input search----------------------- 
    lbl_suche_by_txt.text = NSLocalizedString("search_field", comment: "") 
    lbl_suche_by_txt.textAlignment = .Center 
    lbl_suche_by_txt.backgroundColor = UIColor(white: 1, alpha: 0.5) 
    //---------------------TextField input--------------------------- 
    text_search.delegate = self 
    text_search.resignFirstResponder() 
    text_search.textAlignment = .Center 
    text_search.backgroundColor = UIColor.whiteColor() 
    text_search.placeholder = NSLocalizedString("search_here", comment: "") 
    text_search.keyboardType = UIKeyboardType.Default 
    text_search.returnKeyType = UIReturnKeyType.Done 
    //---------------------txt For alphabetical search----------------------- 
    lbl_such_by_alph.text = NSLocalizedString("search_list", comment: "") 
    lbl_such_by_alph.textAlignment = .Center 
    lbl_such_by_alph.backgroundColor = UIColor(white: 1, alpha: 0.5) 
    //---------------------Table View Alphabet------------------------------- 
    alph_list.delegate = self 
    alph_list.dataSource = self 
    alph_list.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    alph_list.estimatedRowHeight = 50.0 
    alph_list.rowHeight = UITableViewAutomaticDimension 
    //---------------------BG Info Rights------------------------------------ 
    bg_infro.backgroundColor = UIColor(white: 1, alpha: 0.5) 
    txt_info.text = NSLocalizedString("rights", comment: "") 
    txt_info.numberOfLines = 0 
    txt_info.textColor = UIColor.blackColor() 


    let orient = UIApplication.sharedApplication().statusBarOrientation 
    switch orient{ 
    case .Portrait: 
     self.setContstraintsPortrait() 
     break 
    default: 
     self.setContstraintsLandscape() 
     break 
    } 

    navigationBar.addSubview(button_Back) 
    navigationBar.addSubview(titre) 
    self.scroll.addSubview(lbl_suche_by_txt) 
    self.scroll.addSubview(text_search) 
    self.scroll.addSubview(lbl_such_by_alph) 
    self.scroll.addSubview(alph_list) 
    self.scroll.addSubview(bg_infro) 
    self.scroll.addSubview(txt_info) 
    waitView.addSubview(loading_text) 
    waitView.hidden = true 
    self.scroll.addSubview(waitView) 
    self.view.addSubview(scroll) 
    self.view.addSubview(navigationBar) 


} 




override func viewDidAppear(animated: Bool) { 
    alph_list.reloadData() 
    var table_height:CGFloat = 0 
    //----------------Init Values---------------------- 
    let ScreenSize: CGRect = UIScreen.mainScreen().bounds 
    let w = ScreenSize.width 

     for index in 0...items.count - 1 { 
      let IndexPath = NSIndexPath(forRow:index, inSection:0) 
      let cell = alph_list.cellForRowAtIndexPath(IndexPath) 
      if cell != nil { 
       let cell: UITableViewCell = alph_list.cellForRowAtIndexPath(IndexPath)! 
       cell.textLabel?.lineBreakMode = .ByWordWrapping 
       cell.textLabel?.numberOfLines = 0 
       print(cell.textLabel?.text) 
       alph_list.endUpdates() 
       table_height += alph_list.cellForRowAtIndexPath(IndexPath)!.frame.height 
       print(table_height) 
       if #available(iOS 8, *){ 
        table_height += alph_list.cellForRowAtIndexPath(IndexPath)!.frame.height 
       }else{ 
        print(table_height) 
       } 
      } 
     } 
    alph_list.frame = CGRect (x: button_Back.frame.origin.x, y: lbl_such_by_alph.frame.origin.y + lbl_such_by_alph.frame.height + 10, width: w - 20 , height: 26*45) 
    alph_list.contentSize = CGSizeMake(200, (26*45)) 
    //-------------BG info rights-------------------------- 
    bg_infro.frame = CGRect(x: button_Back.frame.origin.x, y: alph_list.frame.origin.y + alph_list.frame.height + 10 , width: w - 20, height: 220) 
    //-------------BG txt rights-------------------------- 
    txt_info.frame = CGRect(x: button_Back.frame.origin.x + 10, y: bg_infro.frame.origin.y + 10 , width: w - 40, height: 250) 
    scroll_h = bg_infro.frame.origin.y + bg_infro.frame.height + 30 
    self.scroll.contentSize = CGSizeMake(w, scroll_h) 
    has_started = true 
} 


func setContstraintsLandscape(){ 

    print("portrait func beständigkeitsliste") 
    //----------------Scroll View----------------------- 
    scroll.frame = UIScreen.mainScreen().bounds 
    //----------------Init Values----------------------- 
    let ScreenSize: CGRect = UIScreen.mainScreen().bounds 
    let w = ScreenSize.width 
    let h = ScreenSize.height 
    let zero: CGFloat = 0 

    //-------------Background--------------------------- 
    imageView.removeFromSuperview() 
    imageView.translatesAutoresizingMaskIntoConstraints = true 
    imageView.frame = CGRectMake(zero,zero, w, h) 
    imageView.image = bg_image 
    self.view.addSubview(imageView) 
    self.view.sendSubviewToBack(imageView) 
    //---------------Navigation Bar-------------------- 
    navigationBar.frame = CGRect(x: 0, y: 0, width: w, height: 60) 
    //--------------Button Back------------------------- 
    button_Back.translatesAutoresizingMaskIntoConstraints = true 
    button_Back.frame = CGRectMake(10, 10, 50, 50) 
    //--------------Title------------------------------- 
    titre.frame = CGRect (x: 0, y: button_Back.frame.origin.y, width: w, height: 50) 
    //-------------Text Search by Text------------------ 
    lbl_suche_by_txt.frame = CGRect(x: button_Back.frame.origin.x, y: button_Back.frame.origin.y + button_Back.frame.height + 10 , width: w - 20, height: 40) 
    //-------------Text Field Search by Text------------------ 
    text_search.frame = CGRect(x: button_Back.frame.origin.x, y: lbl_suche_by_txt.frame.origin.y + lbl_suche_by_txt.frame.height + 10 , width: w - 20, height: 40) 
    //-------------Text Search by Alphabet------------------ 
    lbl_such_by_alph.frame = CGRect(x: button_Back.frame.origin.x, y: text_search.frame.origin.y + text_search.frame.height + 30 , width: w - 20, height: 40) 
    //-------------List Alphabet------------------ 
    var table_height: CGFloat = 0 
    if(has_started == false){ 
     table_height = CGFloat(items.count * 50) 
    }else{ 
     for index in 0...items.count - 1 { 
      let IndexPath = NSIndexPath(forRow:index, inSection:0) 
      let cell = alph_list.cellForRowAtIndexPath(IndexPath) 
      if cell != nil { 
       table_height += alph_list.cellForRowAtIndexPath(IndexPath)!.frame.height 
      } 
     } 
    } 
    alph_list.frame = CGRect(x: button_Back.frame.origin.x, y: lbl_such_by_alph.frame.origin.y + lbl_such_by_alph.frame.height + 10 , width: w - 20, height: table_height) 
    //-------------BG info rights-------------------------- 
    bg_infro.frame = CGRect(x: button_Back.frame.origin.x, y: alph_list.frame.origin.y + alph_list.frame.height + 10 , width: w - 20, height: 220) 
    //-------------BG txt rights-------------------------- 
    txt_info.frame = CGRect(x: button_Back.frame.origin.x + 10, y: bg_infro.frame.origin.y + 20 , width: w - 40, height: 250) 



    //-------------Scroll View Size------------------------------- 
    scroll_h = alph_list.frame.origin.y + table_height + 30 
    self.scroll.contentSize = CGSizeMake(ScreenSize.width, scroll_h) 
    //----------------Wait Screen----------------------- 
    waitView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.scroll.frame.height) 
    loading_text.frame = CGRect(x: (ScreenSize.width - 150)/2, y: (ScreenSize.height - 100)/2, width: 150, height: 100) 


} 

func setContstraintsPortrait(){ 

    print("portrait func liste chemie") 
    //----------------Scroll View----------------------- 
    scroll.frame = UIScreen.mainScreen().bounds 
    //----------------Init Values----------------------- 
    let ScreenSize: CGRect = UIScreen.mainScreen().bounds 
    let w = ScreenSize.width 
    let h = ScreenSize.height 
    let zero: CGFloat = 0 

    //-------------------------------------------------- 
    imageView.removeFromSuperview() 
    imageView.translatesAutoresizingMaskIntoConstraints = true 
    imageView.frame = CGRectMake(zero,zero, w, h) 
    imageView.image = bg_image 
    self.view.addSubview(imageView) 
    self.view.sendSubviewToBack(imageView) 
    //---------------Navigation Bar--------------------- 
    navigationBar.frame = CGRect(x: 0, y: 0, width: w, height: 80) 
    //--------------Button Back------------------------- 
    button_Back.translatesAutoresizingMaskIntoConstraints = true 
    button_Back.frame = CGRectMake(10, 30, 50, 50) 
    //--------------Title------------------------------- 
    titre.frame = CGRect (x: 0, y: button_Back.frame.origin.y, width: w, height: 50) 
    //-------------Text Search by Text------------------ 
    lbl_suche_by_txt.frame = CGRect(x: button_Back.frame.origin.x, y: button_Back.frame.origin.y + button_Back.frame.height + 10 , width: w - 20, height: 40) 
    //-------------Text Field Search by Text------------------ 
    text_search.frame = CGRect(x: button_Back.frame.origin.x, y: lbl_suche_by_txt.frame.origin.y + lbl_suche_by_txt.frame.height + 10 , width: w - 20, height: 40) 
    //-------------Text Search by Alphabet------------------ 
    lbl_such_by_alph.frame = CGRect(x: button_Back.frame.origin.x, y: text_search.frame.origin.y + text_search.frame.height + 30 , width: w - 20, height: 40) 
    //-------------List Alphabet------------------ 
    var table_height: CGFloat = 0 
    if(has_started == false){ 
     table_height = CGFloat(items.count * 50) 
    }else{ 
     for index in 0...items.count - 1 { 
      let IndexPath = NSIndexPath(forRow:index, inSection:0) 
      let cell = alph_list.cellForRowAtIndexPath(IndexPath) 
      if cell != nil { 
       table_height += alph_list.cellForRowAtIndexPath(IndexPath)!.frame.height 
      } 
     } 
    } 
    alph_list.frame = CGRect(x: button_Back.frame.origin.x, y: lbl_such_by_alph.frame.origin.y + lbl_such_by_alph.frame.height + 10 , width: w - 20, height: table_height) 
    alph_list.contentSize = CGSizeMake(w-20, table_height) 

    print(table_height) 

    //-------------BG info rights-------------------------- 
    bg_infro.frame = CGRect(x: button_Back.frame.origin.x, y: alph_list.frame.origin.y + alph_list.frame.height + 10 , width: w - 20, height: 220) 
    //-------------BG txt rights-------------------------- 
    txt_info.frame = CGRect(x: button_Back.frame.origin.x + 10, y: bg_infro.frame.origin.y + 20 , width: w - 40, height: 250) 
    //-------------Scroll View Size------------------------------- 
    scroll_h = alph_list.frame.origin.y + table_height + 30 
    self.scroll.contentSize = CGSizeMake(w, scroll_h) 
    //----------------Wait Screen----------------------- 
    waitView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.scroll.frame.height) 
    loading_text.frame = CGRect(x: (w - 150)/2, y: (h - 100)/2, width: 150, height: 100) 

} 


func textFieldShouldReturn(textField: UITextField) -> Bool { 
    text_search.resignFirstResponder() 
    print("Oki") 
    let listresultview: ListResultsViewController = ListResultsViewController(imageURL: text_search.text) 
    self.presentViewController(listresultview, animated:true, completion: nil) 

    return true 
} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return self.items.count 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell 
    cell.textLabel?.text = self.items[indexPath.row] 
    return cell 
} 


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    waitView.frame = CGRect(x: 0, y: scroll.bounds.origin.y, width: self.view.frame.width, height: self.scroll.frame.height) 
    loading_text.frame = CGRect(x: (UIScreen.mainScreen().bounds.width - 150)/2, y: (UIScreen.mainScreen().bounds.height - 100)/2, width: 150, height: 100) 
    self.waitView.hidden = false 
    dispatch_async(dispatch_get_main_queue()) { 
     self.loadListResultView(indexPath.row) 
    } 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    text_search.resignFirstResponder() 
    self.view.endEditing(true) 
} 

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

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

     return UITableViewAutomaticDimension 
} 

答えて

1

カスタムUITableViewCellのを作成する必要があります。 UITableViewCellに制約を追加すると、私のために解決されました。

+0

どのようにプログラムで制約を追加しますか? (私はプログラム的に制約を避けるために試しましたが、もしそれが唯一の方法であれば...) – nightslit

+0

私はカスタムUITableViewCellを作成することを提案します –

+0

派生uiclassesを作成する方法へのリンクはありますか?私はプリンシペにあまり精通していない。 – nightslit

関連する問題