我試圖以編程方式創建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
我使用的Xcode 7.3,雨燕2.2的iOS 9.3
檢查這... http://stackoverflow.com/questions/26519248/how-to-set-the-full-width -of-separator-in-uitableview –
我盡我所能地重寫了它,沒有任何變化 –
快速檢查中有另一個答案,檢查所有答案 –