2014-09-28 45 views
0

我有一些麻煩我tableviews和細胞對iPhone4S的外觀。在模擬器和新設備中,一切都很好。顯示的TableView和CustomCell煩惱上的iPhone4s

  1. 的UITableViewController 定製表格單元格(圖1)未正確呈現。所有的標籤等都在彼此之上。它們應該像在模擬器上一樣在下面顯示。

  2. ViewController with TableView 其他自定義單元格甚至不會顯示在iPhone4上,只會出現灰色方塊。

我正在使用自動佈局。你有什麼建議嗎?

1 2

下面的代碼: 1圖片:

import UIKit 

class FeedController: TableViewController { 
override func viewDidLoad() { 
    super.viewDidLoad() 

    // adjusting text size without quitting app 
    NSNotificationCenter.defaultCenter().addObserver(self, 
     selector: "onContentSizeChange:", 
     name: UIContentSizeCategoryDidChangeNotification, 
     object: nil) 

    // set table row height 
    tableView.estimatedRowHeight = 89 
    tableView.rowHeight = UITableViewAutomaticDimension 

    // removes the back button after clicked on send button to write a post 
    self.navigationItem.hidesBackButton = true 

    // refresh control 
    self.refreshControl = UIRefreshControl() 
    self.refreshControl!.addTarget(self, action: Selector("refresh:"), forControlEvents: UIControlEvents.ValueChanged) 
    self.refreshControl!.backgroundColor = UIColor.grayColor() 
    self.refreshControl!.tintColor = UIColor.whiteColor() 
    self.tableView.addSubview(refreshControl!) 
} 

func reloadFeed(note: NSNotification){ 
    tableView.reloadData() 
} 

func refresh(sender: AnyObject){ 
    self.refreshControl!.endRefreshing() 
} 

// called when the text size was changed by the user 
func onContentSizeChange(notification: NSNotification) { 
    tableView.reloadData() 
} 

}

import Foundation 
import UIKit 

class TableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource, PDeliversStatusAlerts { 
let notifCenter = NSNotificationCenter.defaultCenter() 

override init() { 
    super.init() 
    notifCenter.addObserver(self, selector: "displayConnectionLostAlert", name: kConnectionLostNotification, object: self) 
} 

required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    notifCenter.addObserver(self, selector: "displayConnectionLostAlert", name: kConnectionLostNotification, object: self) 
} 


func displaySimpleAlert(#title:String, message:String){ 
    var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
    self.presentViewController(alert, animated: true, completion: nil) 
} 

func displayModalDialog(#title: String, message: String, yesHandler: ((UIAlertAction!) -> Void)?, noHandler: ((UIAlertAction!) -> Void)?) { 
    var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: yesHandler)) 
    alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: noHandler)) 

    self.presentViewController(alert, animated: true, completion: nil) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

func push(sender: AnyObject) { 
} 

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

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return false 
} 

第二個畫面:

import UIKit 

class NextViewController: ViewController, UITableViewDelegate, UITableViewDataSource{ 

var followedFeed : FollowedHashtagFeed? 
var hottestFeed  : HottestHashtagFeed? 
var nearbyFeed  : NearbyHashtagFeed? 

var hashtagFeed  : HashtagFeed? 

@IBAction func hottestButtonTapped(sender:AnyObject) { 
    hashtagFeed = FeedFactory.instance().hottestHashtagFeed() 
    notifCenter.removeObserver(self) 
    notifCenter.addObserver(self, selector: "reloadFeed", name: hashtagFeed?.notificationName, object: nil) 
    hashtagFeed!.subscribe() 
    reloadFeed() 
} 

@IBAction func nearbyButtonTapped(sender: AnyObject) { 
    hashtagFeed = FeedFactory.instance().nearbyHashtagFeed() 
    notifCenter.removeObserver(self) 
    notifCenter.addObserver(self, selector: "reloadFeed", name: hashtagFeed?.notificationName, object: nil) 
    notifCenter.addObserver(self, selector: "didReceiveLocationPermissionNeededNotification:", name: "location_permission_needed", object: nil) 
    hashtagFeed!.subscribe() 
    reloadFeed() 
} 

@IBAction func followedButtonTapped(sender: AnyObject) { 
    hashtagFeed = FeedFactory.instance().followedHashtagFeed() 
    notifCenter.removeObserver(self) 
    notifCenter.addObserver(self, selector: "reloadFeed", name: hashtagFeed?.notificationName, object: nil) 
    hashtagFeed!.subscribe() 
    reloadFeed() 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 


    //set table row height 
    tableView.rowHeight = UITableViewAutomaticDimension 

    //load feed cell 
    var nipName=UINib(nibName: "NextTableCell", bundle:nil) 
    self.tableView.registerNib(nipName, forCellReuseIdentifier: "nextCell") 

    followedButtonTapped(followedButton) 


    view.setNeedsLayout() 
    view.layoutIfNeeded() 

    println("Frame Height:") 
    println(tableView.frame.height) 
    println(tableView.bounds.height) 
    println("Frame Width:") 
    println(self.tableView.frame.width) 
    println(self.tableView.bounds.width) 

    /* 
    hashtagFeed = FeedFactory.instance().followedHashtagFeed() 

    //subscribe to feed changes 
    notifCenter.addObserver(self, selector: "reloadFeed", name: hashtagFeed?.notificationName, object: nil) 
    hashtagFeed!.subscribe()*/ 
} 

func didReceiveLocationPermissionNeededNotification(note: NSNotification){ 
    displayModalDialog(
     title:  "Location Permission Needed", 
     message: "In order to use this functionality the app needs your permission to use location data - do you want to give this permission now?", 
     yesHandler: { 
      (action: UIAlertAction!) in LocationHelper.instance().askForPermission() 
     }, 
     noHandler: nil 
    ) 
} 



//Tableview 

func reloadFeed(){ 
    tableView.reloadData() 
} 

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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("nextCell", forIndexPath: indexPath) as NextTableCell 
    let hashtag = hashtagFeed!.toArray()[indexPath.row] 
    cell.loadItem(hashtag) 
    return cell 
} 

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

}

+0

你有任何的代碼? – rocky 2014-09-28 20:07:06

+0

我編輯了我的問題,你需要嗎?其他?謝謝! – Joe 2014-09-29 14:36:21

回答

0

當爲單元格子視圖設置的約束不強制單元格的特定高度時,可能會出現此類問題。

例如,如果您有一個包含單個子視圖的單元格,並且子視圖對它的超級視圖(最近鄰居,即單元格的contentView)的頂點/底部/左/右約束爲10px,但子視圖本身會沒有高度限制,單元格不能正確佈局。

這可以像所有的細胞在彼此的頂部,或帶有默認44px高度的所有單元格。

要解決此問題(如果我們用一個單一的子視圖細胞的例子視圖層次的工作),定義子視圖高度約束。您可能還需要高度約束的優先級設置爲東西小於1000

不知道這是否會解決您的具體問題,但它是固定的類似問題對我來說,所以在這裏提出它作爲一個選項。

對於它的價值,我相當肯定,你的問題是制約有關,這意味着上述不會真正幫助回答這個問題的代碼。

+0

我認爲它也與約束有關。但奇怪的是,它對iPhone5或更新的設備絕對正常。單元格和表格視圖顯示正確。只有在iphone4上,這些單元格纔會顯示爲我想要的格式。即使在模擬器(iphone4s)上單元格也顯示正確。 – Joe 2014-09-29 15:20:53