简体   繁体   English

UItableView plist数据排序

[英]UItableView plist data ordering

Hi I have created a table view which filled with a plist data , I need re ordering the cells , I mean instead of showing data like this : 嗨,我创建了一个表格视图,其中填充了plist数据,我需要对单元格重新排序,我的意思是不要显示如下数据:

1
2
3
4
5

should be like this : 应该是这样的:

5
4
3
2
1

I am using this code : 我正在使用此代码:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
    titles =  [[titles sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]] retain]; 

in this method : 在这种方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

but the result is something like this 但结果是这样的

1
5
2
4
3 

how can I fix it ? 我该如何解决?

EIDTED : 内容:

my plist : 我的清单:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>1</string>
    <string>2</string>
    <string>3</string>
    <string>4</string>
    <string>5</string>
</array>
</plist>

change ascending:NO to ascending:YES when creating the sort descriptor. 创建排序描述符时,将ascending:NO更改为ascending:YES

To provide any more advice you'd probably need to post more code. 要提供更多建议,您可能需要发布更多代码。 You should post how you're actually populating the data structure which acts as your data source, and how you are using that data structure in cellForRowAtIndexPath . 您应该在cellForRowAtIndexPath发布如何实际填充用作数据源的数据结构,以及如何使用该数据结构。

Format your data like this (note that you can do this by creating a .plist with an array in it) 像这样格式化数据(请注意,您可以通过创建带有数组的.plist来做到这一点)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Title of Array</key>
    <array>
        <string>1</string>
        <string>2</string>
        <string>3</string>
        <string>4</string>
        <string>5</string>
    </array>
</dict>
</plist>

Then use this code to get it 然后使用此代码获取它

NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];

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

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