在iPhone 4s上显示表视图和自定义单元故障

时间:2014-09-28 17:06:17

标签: ios uitableview uiviewcontroller xcode6gm

我在iphone4s上的tableviews和cell的外观上有些麻烦。在模拟器和新设备中,一切都很好。

  1. 的UITableViewController 自定义表格单元格(图片1)未正确显示。所有标签等都位于彼此之上。它们应该像在模拟器上一样显示在另一个之下。

  2. ViewView与TableView 其他自定义单元格甚至不会在iPhone4上显示只显示灰色方块。

  3. 我正在使用自动布局。你有什么建议吗?

    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!) {
    }
    

    }

1 个答案:

答案 0 :(得分:0)

当为单元格的子视图设置的约束不强制执行单元格的特定高度时,这种问题就会出现。

例如,如果您有一个包含单个子视图的单元格,并且子视图对其10px的超级视图(最近邻居,即单元格的contentView)具有上/下/左/右约束,但是子视图本身没有高度约束,单元格将无法正确布局。

这可能看起来像所有单元格在彼此之上,或者所有单元格都以默认的44px高度呈现。

要解决此问题(如果我们正在使用具有单个子视图的单元格的示例视图层次结构),请为子视图定义高度约束。您可能还需要将高度约束的优先级设置为小于1000的值。

不确定这是否能解决您的具体问题,但它已为我修复了类似问题,因此请将其作为选项提供。

对于它的价值,我相当肯定你的问题与约束有关,这意味着上面的代码不会真正帮助回答这个问题。