2017-02-27 193 views
1

我是新來的swift和ios開發的一般。我在tableview中顯示json。由於某些原因,tableview不滾動到底部(3個單元格)。 這裏是我的UITableView的代碼的實現代碼如下所在:Tableview不滾動到底部

class AllChaptersViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

@IBOutlet weak var tableView: UITableView! 
var isLoadingTableView = true 
var chapters = [Chapter]() //Chaper Array 
var chapter = [ChapterMO]() 
var imageURLs = [String]() 
var chaptersArray = [AnyObject]() 
var base = [AnyObject]() 
var uri = "" 
var path = "" 
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

override func viewDidLoad() { 
    super.viewDidLoad() 

    Alamofire.request("https://appdata.omicronenergy.com/webindex/COMP-en.json").responseJSON { response in debugPrint(response) 
     let result = response.result 
     if let dict = result.value as? Dictionary<String,AnyObject>{ 
      if let arr = dict["data"] as! NSArray?{ 
       self.chaptersArray = arr as [AnyObject] 
       self.tableView.reloadData() 
      } 
     } 
    } 


    Alamofire.request("https://appdata.omicronenergy.com/webindex/products-en.json").responseJSON { 
     response in debugPrint(response) 
     let result = response.result 
     if let json = result.value as? Dictionary<String,AnyObject>{ 
      if let arr = json["data"] as! NSArray? { 
       let tmpJson = arr[0] as! [String:AnyObject] 
       self.uri = tmpJson["uri"] as! String 
      } 
     } 
    } 

    tableView.layoutMargins = .zero 
    tableView.separatorInset = .zero 
    tableView.contentInset = .zero 
    self.automaticallyAdjustsScrollViewInsets = false 
    tableView.tableFooterView = UIView() 
    self.tableView.rowHeight = UITableViewAutomaticDimension 
    self.tableView.reloadData() 

    // Read qr code 

    let qrCodeButton = UIBarButtonItem(image: UIImage(named:"qrCode"), 
             style: .plain, 
             target: self, 
             action: #selector(AllChaptersViewController.readQrCode(_:))) 

    let moreButton = UIBarButtonItem(image: UIImage(named:"more"), 
            style: .plain, 
            target: self, 
            action: #selector(AllChaptersViewController.openDropDownMenu(_:))) 

    self.navigationItem.rightBarButtonItems = [moreButton,qrCodeButton] 

    } 

override var prefersStatusBarHidden: Bool{ 
    return true 
} 

func readQrCode(_ sender:UIBarButtonItem!) { 
    print("working") 
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "QRCodeViewController") 
    self.navigationController!.pushViewController(vc!, animated: true) 
    print("") 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

func openDropDownMenu(_ sender:UIBarButtonItem!) { 
    print("works too") 
} 


override func viewWillAppear(_ animated: Bool) { 
    self.navigationItem.title = "COMPANO 100" 
    self.tableView.layoutSubviews() 
} 


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

func getData(){ 
    do{ 
     chaptersArray = try context.fetch(ChapterMO.fetchRequest()) 
    } 
    catch { 
     print("Fetching failed") 
    } 
} 



func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "chapterCell", for: indexPath) as! TableViewCell 
    let object = chaptersArray[indexPath.row] as! NSDictionary 

    let path = object.object(forKey: "path") as! String 
    let iconUrl = "https://appdata.omicronenergy.com/chapter_icons/COMP0\(path).png" 

    cell.icon.sd_setImage(with: URL(string:iconUrl)) 
    cell.layoutMargins = .zero 
    let name = object.object(forKey: "name") as! String 
    cell.chapterName?.text = name 
    return cell 
} 

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let path = chaptersArray[indexPath.row]["path"] 
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChapterViewController") as! ChapterViewController 
    let baseURL = uri 
    let title = chaptersArray[indexPath.row]["name"] 
    vc.title = title as! String 
    vc.baseURL = baseURL 
    vc.path = path! as! String 
    self.navigationController?.pushViewController(vc, animated: true) 
} 

}

+0

檢查與_uitableview_ –

回答

0

這一定是的tableView 的高度,以確保我的想法在ViewDidLoad,就把這行

self.tableView.frame = CGRect(x: self.tableView.frame.origin.x, y: self.tableView.frame.origin.y, width: self.tableView.frame.size.width, height: self.view.frame.size.height - self.tableView.frame.origin.y) 

這會使tableView端的高度在self.view之內,不在其外面

的想法是檢查的tableView這一切似乎在它的容器,給的tableView背景不同的顏色更好看

+0

1.5行的自動佈局不滾動 –

+0

什麼是1.5究竟 –

+0

是的tableView中self.view或它的另一個視圖 –

0

我不明白你的問題
的tableview不自動滾動到下當負載完成 或
拉起時不能滾動到底部?

注意:如果您爲uitableview設置了禁忌,則無法爲其設置名氣。但如果你想做到這一點,請嘗試:

yourView.translatesAutoresizingMaskIntoConstraints = true 
+0

無法滾動到最後3行 –

+0

檢查:在可視視圖的故事板和約束條件下的底部條 –