通过plist(不是textLabel的名称)对UITableViewCell进行排序

时间:2011-05-05 22:28:41

标签: iphone uitableview nsarray nssortdescriptor

所有新代码,我都不知道为什么这不起作用。它使用我的所有数据加载表格,而不仅仅按字母顺序排列......

更新:添加了数据结构:

<?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>
    <dict>
        <key>Rows</key>
        <array>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>Address</string>
                    <string>Tuesday</string>
                    <string>Information</string>
                    <integer>36</integer>
                    <integer>-118</integer>
                    <string>Place LA</string>
                    <string>California</string>
                </array>
                <key>Title</key>
                <string>California</string>
            </dict>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>Address</string>
                    <string>Sunday</string>
                    <string>Information</string>
                    <integer>40</integer>
                    <integer>-72</integer>
                    <string>NY Place</string>
                    <string>New York</string>
                </array>
                <key>Title</key>
                <string>New York</string>
            </dict>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>Address</string>
                    <string>Sunday</string>
                    <string>Information</string>
                    <integer>30</integer>
                    <integer>-97</integer>
                    <string>Austin Place</string>
                    <string>Texas</string>
                </array>
                <key>Title</key>
                <string>Texas</string>
            </dict>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>Address</string>
                    <string>Wednesday</string>
                    <string>Information</string>
                    <integer>30</integer>
                    <integer>-97</integer>
                    <string>Cleveland Place</string>
                    <string>Ohio</string>
                </array>
                <key>Title</key>
                <string>Ohio</string>
            </dict>
        </array>
    </dict>
</array>
</plist>


- (void)viewDidLoad {
     [super viewDidLoad];
     if (!self.tableData)
     {
         [self sortedArray];
         self.title = @"Title"
     }
}


-(NSArray *)sortedArray
{
    NSString *path = [[NSBundle 
                       mainBundle]pathForResource:@"Data3" ofType:@"plist"];
    tableData = [NSArray arrayWithContentsOfFile:path];
    NSArray *rows = [[tableData objectAtIndex:0]objectForKey:@"Rows"];
    NSArray *rows2 = [[rows objectAtIndex:0]objectForKey:@"Subtree"];
    NSString *states = [NSString stringWithFormat:@"%@",[rows2 objectAtIndex:6]];
    NSString *days = [NSString stringWithFormat:@"%@",[rows2 objectAtIndex:1]];
    NSSortDescriptor *stateDescriptor = [[NSSortDescriptor alloc]initWithKey:states ascending:YES];
    NSSortDescriptor *dayDescriptor = [[NSSortDescriptor alloc]initWithKey:days ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:stateDescriptor, dayDescriptor, nil];
    NSArray *finalArray = [tableData sortedArrayUsingDescriptors:sortDescriptors];
    return finalArray;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:24];
    cell.textLabel.text = [[[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row] objectForKey: @"Title"];
    cell.detailTextLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:14];
    cell.detailTextLabel.text = [[[[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row] objectForKey: @"Subtree"]objectAtIndex:0];
    return cell;
}

1 个答案:

答案 0 :(得分:0)

在视图中加载,也许

self.tableDate = [self sortedArray];

在您的sortedArray方法中,您正在对数据进行排序并将其返回,但从不将已排序的数组分配给任何内容。

您正在sortArray方法中将未排序的数据分配给tableData,这就是您获取未排序数据的原因。我会把这个任务拿出来,这很令人困惑。