2016-03-31 77 views
0

我試圖以編程方式創建UITableView。我有一個單元格寬度的問題,它固定在320,而桌子寬度爲iphone 6的375或iphone 6的加上爲什麼UITableViewCell寬度小於UITableView寬度

我如何使單元格寬度等於表的寬度?

視圖類:

import UIKit 

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let myTableView = UITableView(frame: self.view.frame) 
     myTableView.delegate = self 
     myTableView.dataSource = self 
     view.addSubview(myTableView) 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

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


    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell:customTableViewCell = customTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "mycell") 
     cell.backgroundColor = UIColor.blueColor() 
     print("tableView width is \(tableView.frame.size.width)") 
     return cell 
    } 
} 

定製細胞類:

import UIKit 

class customTableViewCell: UITableViewCell { 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     configureView() 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     configureView() 
    } 

    func configureView() { 

     print("cell width is \(self.frame.size.width)") 
     print("cell bounds is \(self.contentView.frame.width)") 
     let locationLabel = UILabel() 
     locationLabel.frame = CGRectMake(0, 0 , self.frame.size.width,self.frame.size.height) 
     locationLabel.text = "custom cell" 
     locationLabel.backgroundColor = UIColor.blackColor() 

     self.addSubview(locationLabel) 
     // add and configure subviews here 
    } 

} 

結果運行代碼時:

單元寬度是320.0
細胞界限是320.0
的tableView寬度是414

screenshot

我使用的Xcode 7.3,雨燕2.2的iOS 9.3

+0

檢查這... http://stackoverflow.com/questions/26519248/how-to-set-the-full-width -of-separator-in-uitableview –

+0

我盡我所能地重寫了它,沒有任何變化 –

+0

快速檢查中有另一個答案,檢查所有答案 –

回答

-1

也許你應該明確地設置單元格的寬度,它現在有一個默認大小:

編輯

更改cell class:

import UIKit 

class customTableViewCell: UITableViewCell { 

    var locationLabel: UILabel = UILabel() 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     configureView() 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     configureView() 
    } 

    func configureView() { 

     print("cell width is \(self.frame.size.width)") 
     print("cell bounds is \(self.contentView.frame.width)") 
     locationLabel.text = "custom cell" 
     locationLabel.textColor = UIColor.whiteColor() 
     locationLabel.backgroundColor = UIColor.blackColor() 

     self.addSubview(locationLabel) 
     // add and configure subviews here 
    } 

} 

編輯在VC:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell:customTableViewCell = customTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "mycell") 
     cell.backgroundColor = UIColor.blueColor() 
     cell.frame = CGRectMake(0, 0, tableView.frame.size.width, cell.frame.size.height) 
     cell.locationLabel.frame = cell.frame 
     print("cell width in cellForRowAtIndexPath is \(cell.frame.size.width)") 
     print("tableView width is \(tableView.frame.size.width)") 
     return cell 
    } 

輸出:

cell width is 320.0 
cell bounds is 320.0 
cell width in cellForRowAtIndexPath is 414.0 
tableView width is 414.0 
+0

我試着將寬度設置爲tableview類的單元格框架,邊界,contentview。大小仍然320. –

+0

@DanTImofte檢查我編輯的答案 – schmidt9

+0

謝謝@ schmidt9,你的答案提供了一個很好的解決我遇到的問題。 –

相關問題