【问题标题】:Table View only loads if I put phone in landscape mode仅当我将手机置于横向模式时才会加载表格视图
【发布时间】:2016-04-01 03:35:26
【问题描述】:

我有一个自定义表格视图,当我运行应用程序时,它显示为空白,就像它尚未加载一样,但是当我将手机倾斜到横向模式时,出现条目,这对我来说真的没有意义。有什么建议吗?

编辑:这是我的代码

 import UIKit
 import Alamofire
 import ObjectMapper
 class LotteryTableViewController: UITableViewController {
let lotteryMachine = LotteryMachine()

var currentStandings: [Team] = []
var draftStandings: [Team] = []
override func viewDidLoad() {
    super.viewDidLoad()

    let headers = [
        "User-agent": "LotteryMachine/1.0 (nilayneeranjun24@gmail.com)",
               ]
        Alamofire.request(.GET, "https://erikberg.com/nba/standings.json",headers: headers)
            .responseJSON { response in
                let parentJson = Mapper<Standings>().map(response.2.value)
               let standingsArray: [Team] = parentJson!.standing!
                    self.currentStandings=standingsArray
                    self.draftStandings=self.lotteryMachine.setPossibleCombinations(standingsArray)
                    self.draftStandings=self.lotteryMachine.setDraftPositions(self.draftStandings)
                    print (self.draftStandings.toJSON())
                }

    }




    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return draftStandings.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "LotteryTableViewCell"


    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! LotteryTableViewCell


   let position = self.draftStandings[indexPath.row].draftingPosition!

    let teamName = self.draftStandings[indexPath.row].lastName!
   let record = String(self.draftStandings[indexPath.row].won!) + "-" + String(self.draftStandings[indexPath.row].lost!)

    let player = "Ben Simmons"
    cell.position.text = String(position)
    cell.teamName.text = teamName
    cell.record.text = String(record)
    cell.player.text = player
   cell.teamLogo.image = UIImage(named: "lakers")



    // Configure the cell...

    return cell
}


/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

【问题讨论】:

  • 你检查过它的 CSS 吗?通常在 CSS 中,我们可以使用媒体查询隐藏特定横向/纵向模式的元素。
  • 您是如何设置约束的?尝试trailing space 而不是leading space
  • 您是否在viewWillAppear 方法中重新加载了表格视图?
  • 你的意思是导航工具栏在那里并且有空行(但分隔线),还是纯白色?
  • 有分隔线,没有导航工具栏

标签: ios arrays swift tableview


【解决方案1】:

Alamofire.request 本质上是异步的。因此,当表第一次加载时,请求仍在执行,您的 draftStandings 没有任何数据。当您旋转时,表格会重新加载,到那时 draftStandings 确实已经有一些表格显示的数据。

在请求响应中设置draftStandings 后尝试添加tableView.reloadData()

【讨论】:

  • 是的,就是这样。谢谢
  • 可以说我想在加载选项卡之前调用 http 请求并将standingsArray 传递给我的两个选项卡。我怎么能做到这一点?
  • 我不建议在加载用户界面之前等待 n/w 操作完成(如果您继续执行此类实现,则必须这样做)。始终首选异步调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 2020-08-29
  • 1970-01-01
  • 2023-03-28
相关资源
最近更新 更多