[英]Custom cells in table view not displaying content
我是自動布局和iOS的新手,因此,我想創建Facebook新聞訂閱頁面,以便親自使用自動布局。 我正在使用帶有自定義單元格的表格視圖。 到目前為止完成的工作是:
在情節提要中刪除了一個tableview。
在相應的控制器類中創建其插座。
為自定義單元格創建了控制器類。
在兩個單元格中刪除必要的視圖,並在必要的控制器類中在那里創建出口。
在每個自定義單元格中設置適當的和標識符。
現在,我面臨的問題是兩個單元格都在界面生成器中正確顯示,但是在設備或模擬器中僅第一個單元格顯示。 由於我的聲譽,這里有圖片。 界面構建器
我搜索了很多,但沒有找到合適的答案。 我哪里錯了? 我有想念嗎? 顯然沒有自動布局問題。
例如,我也粘貼了控制器類的代碼。
class HomeScreen: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath.row == 0){
return 100
} else {
return 200
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
@available(iOS 2.0, *)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
@available(iOS 2.0, *)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
guard let cell : StatusUpdateCustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "StatusUpdateCell" , for: indexPath) as? StatusUpdateCustomTableViewCell else {
fatalError("The dequeued cell is not an instance of MealTableViewCell.")
}
cell.profilePicImageView.image = #imageLiteral(resourceName: "teenGirl")
return cell
} else if indexPath.row == 1 {
guard let cell : PostCustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "PostCell1" , for: indexPath) as? PostCustomTableViewCell else {
fatalError("The dequeued cell is not an instance of MealTableViewCell.")
}
cell.profilePicImageView.image = #imageLiteral(resourceName: "teenBoy")
return cell
} else {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "thirdCustomCell")
//set the data here
return cell
}
}
@IBOutlet weak var headerViewHeight: NSLayoutConstraint!
@IBOutlet weak var imageView1: UIImageView!
@IBOutlet weak var imageView2: UIImageView!
@IBOutlet weak var imageView3: UIImageView!
@IBOutlet weak var imageView4: UIImageView!
@IBOutlet weak var imageView1Height: NSLayoutConstraint!
@IBOutlet weak var imageView2Height: NSLayoutConstraint!
@IBOutlet weak var imageView3Height: NSLayoutConstraint!
@IBOutlet weak var imageView4Height: NSLayoutConstraint!
@IBOutlet weak var tableViewPosts: UITableView!
var windowWidth = 0.0;
var windowHeight = 0.0;
override func viewDidLoad() {
super.viewDidLoad()
tableViewPosts.delegate = self
tableViewPosts.dataSource = self
// calling rotated function when device orientation changes.
NotificationCenter.default.addObserver(self, selector: Selector("rotated"), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
override func viewDidLayoutSubviews() {
//print(self.view.frame.size)
windowWidth = Double(self.view.frame.width);
windowHeight = Double(self.view.frame.height);
}
func rotated(){
if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){
// hiding imageview and underlining textfields
print("landscape")
headerViewHeight.constant = CGFloat(windowHeight * 0.07)
imageView1Height.constant = CGFloat(windowHeight * 0.07)
imageView2Height.constant = CGFloat(windowHeight * 0.07)
imageView3Height.constant = CGFloat(windowHeight * 0.07)
imageView4Height.constant = CGFloat(windowHeight * 0.07)
}
if(UIDeviceOrientationIsPortrait(UIDevice.current.orientation)){
headerViewHeight.constant = CGFloat(windowHeight * 0.001)
imageView1Height.constant = CGFloat(windowHeight * 0.001)
imageView2Height.constant = CGFloat(windowHeight * 0.001)
imageView3Height.constant = CGFloat(windowHeight * 0.001)
imageView4Height.constant = CGFloat(windowHeight * 0.001)
}
}
}
從您的代碼中可以看到,您已經實現了numberOfRowsInSection兩次,在其中一個實例中,您返回10,而在另一個實例中,您僅返回1。
在您的代碼中,您將返回1行,因此
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
@available(iOS 2.0, *)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1// It's Wrong
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
@available(iOS 2.0, *)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
您可能正在使用Swift3。返回1的方法適用於Swift 3,但witch返回的方法10不適用。該方法將被視為實例方法(對於您的控制器而言是本地的)。 因此,實際上您的表將僅顯示1行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.