为什么UITableView中的部分在不同设备上的顺序不同?

bee*_*eef 0 iphone uitableview ios swift

我在我的应用程序中实现了一个UITableViewController.除了TableView中的部分顺序外,一切正常.

每个部分中的部分数和行数都可以.我从服务器获取一些值并在TableView中显示它们.它们按日期排序,每个部分包含与该日期相关的值.

现在,如果我有昨天(2014年11月11日)和今天(2014年11月12日)的价值,我的iPhone 6上首先显示12.11.2014部分.在iPhone 5上,首先显示11.11.2014部分 - 但它是相同的代码!我不知道如何解决这个问题.

这是2个截图,所以你知道我的意思:

iPhone 5截图 iPhone 5截图

iPhone 6截图 iPhone 6截图

在第二个屏幕截图中,首先显示12.11.2014.

编辑:
我的TableView显示最新的比特币交易.我有一个NSMutableDictionary,有两个条目(在我的例子中),一个条目为"12.11.2014",一个条目为"11.11.2014",所以我的numberOfSections -method返回2.

var trades : NSMutableDictionary!

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return trades.count
}
Run Code Online (Sandbox Code Playgroud)

现在,该字典中的每个条目都包含一个交易列表,因此我的字典类型只是:

String : [Trade]
Run Code Online (Sandbox Code Playgroud)

所以我的numberOfRowsInSection看起来像这样(我知道它有点棘手):

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (self.trades.objectForKey((self.trades.allKeys as NSArray).objectAtIndex(section) as String)! as [Trade]).count
}
Run Code Online (Sandbox Code Playgroud)

就像我说的,在iPhone 6上它运行良好,在iPhone 5上没有.

Fog*_*ter 5

您将"部分"数据存储在NSDictionary.

NSDictionary是一个无序的集合.在NSDictionary中没有"order"这样的东西.说订单改变没有意义.

如果你想将东西存储在字典中并以相同的顺序将它们输出,那么你需要先对密钥数组进行排序,self.trade.allKeys然后才能解决问题.

这样做并不是不同的设备.您可能会发现它也会在单个设备上发生变化.

更好(不同)的方法是使用NSArray来存储数据.

像这样...

//self.allTrades array...
[
    {
        title  : 11.11.2014,
        trades : //array of trades for 11.11.2014
    },
    {
        title  : 12.11.2014,
        trades : //array of trades for 12.11.2014
    }
]
Run Code Online (Sandbox Code Playgroud)

现在您可以通过以下方式访问某个部分的交易信息...

self.allTrades[indexPath.section]
Run Code Online (Sandbox Code Playgroud)

并访问一个项目......

//            1.                 2.        3.
self.allTrades[indexPath.section]["trades"][indexPath.row]
// 1. get the dictionary from the array for the section
// 2. then get the trades array from that dictionary
// 3. then get the item from that array.
Run Code Online (Sandbox Code Playgroud)