简体   繁体   English

在iPhone4s上显示TableView和CustomCell问题

[英]Displaying TableView and CustomCell troubles on iPhone4s

I have some trouble with the appearance of my tableviews and cells on the iphone4s. 我在iphone4上的表视图和单元格的外观有些麻烦。 Within the simulator and on new devices everything is fine. 在模拟器内以及在新设备上,一切都很好。

  1. UITableViewController The custom table cell (picture 1) isn't presented correctly. UITableViewController自定义表格单元格(图1)显示不正确。 All the labels etc. lie on top of each other. 所有标签等相互叠放。 They should be shown one below the other like they do on the simulator. 应该像在模拟器上一样,将它们显示在一个下方。

  2. ViewController with TableView The other custom cells even don't show on the iPhone4s only a gray square appears. 带有TableView的ViewController其他自定义单元格甚至在iPhone4s上也不显示,仅显示一个灰色方块。

I'm using Auto Layout. 我正在使用自动版式。 Do you have any suggestions? 你有什么建议吗?

1个2

Here's the code: 1st picture: 代码如下:第一张图片:

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
}

2nd picture: 第二张图片:

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

} }

This sort of issue can present itself when the constraints set up for the cell's subviews do not enforce a specific height for the cell. 当为单元格的子视图设置的约束未为单元格强制设置特定高度时,会出现此类问题。

For example, if you have a cell that contains a single subview, and the subview has top/bottom/left/right constraints to its superview (nearest neighbor, ie the cell's contentView) of 10px, but the subview itself does not have a height constraint, the cell will not be laid out properly. 例如,如果您有一个包含单个子视图的单元格,并且该子视图对其上级视图(最近邻居,即单元格的contentView)的上/下/左/右约束为10px,但子视图本身没有高度约束,该单元将无法正确布局。

This can look like all cells on top of one another, or all cells presented with the default 44px height. 这看起来像所有单元格彼此重叠,或者所有单元格的默认高度均为44px。

To fix the issue (if we're working with the example view hierarchy of a cell with a single subview), define a height constraint for the subview. 要解决此问题(如果我们正在处理具有单个子视图的单元格的示例视图层次结构),请为子视图定义高度限制。 You might also need to set the height constraint's priority to something less than 1000. 您可能还需要将高度限制的优先级设置为小于1000的优先级。

Not sure if this will fix your specific issue, but it's fixed similar issues for me, so presenting it here as an option. 不知道这是否可以解决您的特定问题,但是对我来说已经解决了类似的问题,因此请在此处进行介绍。

For what it's worth, I'm fairly certain that your issue is constraints-related, which means the code above wont really help answer the question. 就其价值而言,我可以肯定地说您的问题与约束相关,这意味着上面的代码将无法真正帮助您回答问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM