【问题标题】:How to sort NSarray in Sections for UITableview如何在 UITableview 的部分中对 NSarray 进行排序
【发布时间】:2012-05-03 11:21:05
【问题描述】:

您好,我如何按 Day 对我的 Array 进行排序,我从我的网络服务器 (Json) 获取数据并解析到 NSDictionary 然后保存到 NSMutableArray... 我知道我需要实现

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  • titleForHeaderInSection:
  • 部分行:

NSMutableArray 日志

2012-04-26 06:12:51.256 passingdata[91699:fb03] array : (
{
post =         {
    "CLASS_LEVEL" = "Intro/General";
    "CLASS_TYPE" = "Muay Thai";
    "DAY_OF_WEEK" = Sunday;
    ID = 19;
    "ORDER_BY" = 5;
    TIME = "1:00pm - 2:30pm";
};
}
{
post =         {
    "CLASS_LEVEL" = "General/Intermediate/Advanced";
    "CLASS_TYPE" = "Muay Thai Spar - Competitive";
    "DAY_OF_WEEK" = Friday;
    ID = 27;
    "ORDER_BY" = 5;
    TIME = "6:00pm - 9:00pm";
};
},
{
post =         {
    "CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
    "CLASS_TYPE" = "Fighters Training";
    "DAY_OF_WEEK" = Monday;
    ID = 1;
    "ORDER_BY" = 1;
    TIME = "9:30am - 11:00pm";
};
},

我可以在我的桌子上显示所有数据,但我想每天(周一,周二,周三..周日)按部分进行,我尝试了 NSpredicate 和 NSSet 但仍然无法正常工作。

我不知道如何按天对项目进行分组并获取所有信息以实现所有方法。

有什么想法吗??谢谢

【问题讨论】:

    标签: iphone xcode uitableview


    【解决方案1】:

    您应该使用NSPredicate 类。

    这是你应该做的:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DAY_OF_WEEK MATCHES %@",@"Monday"];
    NSArray *sections = [tableData filteredArrayUsingPredicate:predicate];
    

    谓词格式不应硬编码一天,而应循环遍历所有天并创建包含每天结果的数组。

    【讨论】:

    • 但是我每天都需要一个 NSPredicate 吗?
    • 就像我告诉你的那样,你应该遍历这些天以过滤 numberOfSectionsInTableView,或者创建一个包含每天项目的数组。选择哪种方法取决于您。
    • 我可以每天做谓词,但是我如何从所有数组(MonArray ... SunArray。)中获取信息,谢谢你的帮助我对编程很陌生..谢谢
    【解决方案2】:

    使用 NSSortdiscriptor 代码使用这行代码。

     NSSortDescriptor *timeSD = [NSSortDescriptor sortDescriptorWithKey: @"time" ascending: YES];
     NSMutableArray *sortedByTime = [UnsortedArray sortedArrayUsingDescriptors: timeSD];
     NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:[sortedByTime count]];
    

    【讨论】:

      【解决方案3】:

      此方法是完成工作的最简单方法:

      NSArray *sortedStrings = [strings sortedArrayUsingSelector:@selector(compare:)];
      

      更多关于:sortedArrayUsingSelector

      【讨论】:

        【解决方案4】:

        使用这种方法

        - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
        {
           NSMutableSet *mySet = [[[NSMutableSet alloc] init] autorelease];
           for ( NSString *s in myArray )
           {
             if ( s.length > 0 )
               [mySet addObject:[s substringToIndex:1]];
           }
        
           NSArray *indexArray = [[mySet allObjects] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
           return indexArray;
        }
        

        【讨论】:

        • 错误在这一行 NSArray *indexArray = [[mySet allObjects]..... [NSmutable count] : Method sent to an uninitialized mutable set Object .. 谢谢
        • 如果这个数组发生了,那么你没有在你的 mySet 声明语句中使用 init
        • 对不起..我忘记了我正在使用的初始化(ARC)..我稍后再试谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多