簡體   English   中英

在iPhone4s上顯示TableView和CustomCell問題

[英]Displaying TableView and CustomCell troubles on iPhone4s

我在iphone4上的表視圖和單元格的外觀有些麻煩。 在模擬器內以及在新設備上,一切都很好。

  1. UITableViewController自定義表格單元格(圖1)顯示不正確。 所有標簽等相互疊放。 應該像在模擬器上一樣,將它們顯示在一個下方。

  2. 帶有TableView的ViewController其他自定義單元格甚至在iPhone4s上也不顯示,僅顯示一個灰色方塊。

我正在使用自動版式。 你有什么建議嗎?

1個2

代碼如下:第一張圖片:

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

}

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

例如,如果您有一個包含單個子視圖的單元格,並且該子視圖對其上級視圖(最近鄰居,即單元格的contentView)的上/下/左/右約束為10px,但子視圖本身沒有高度約束,該單元將無法正確布局。

這看起來像所有單元格彼此重疊,或者所有單元格的默認高度均為44px。

要解決此問題(如果我們正在處理具有單個子視圖的單元格的示例視圖層次結構),請為子視圖定義高度限制。 您可能還需要將高度限制的優先級設置為小於1000的優先級。

不知道這是否可以解決您的特定問題,但是對我來說已經解決了類似的問題,因此請在此處進行介紹。

就其價值而言,我可以肯定地說您的問題與約束相關,這意味着上面的代碼將無法真正幫助您回答問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM