如何在UITableview的Sections中对NSarray进行排序

时间:2012-05-03 11:21:05

标签: iphone xcode uitableview

您好我如何按天分类我的数组,我从我的网络服务器(Json)获取数据并解析为NSDictionary然后保存到NSMutableArray ...我明白我需要实现

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

NSMutableArray Log

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但仍然没有工作。

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

任何想法?感谢

4 个答案:

答案 0 :(得分:1)

您应该使用NSPredicate类。

这是你应该做的:

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

谓词格式不应该对当天进行硬编码,而应该遍历所有日期并创建包含每天结果的数组。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

使用这种方法

- (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;
}

答案 3 :(得分:0)

这种方法是最简单的工作方式:

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

更多关于:sortedArrayUsingSelector