我想为每个唯一的日期在UITableView
中创建一个部分。
我的数据被格式化为对象数组,每个对象内部都包含一个NSDate
。
最初,我已经使用dateFormatter @“yyyy-MM-dd”将每个日期转换为字符串,并相应地比较了日期字符串。但这很慢,似乎是错误的方法。
我还尝试使用以下方法从NSDate
中删除时间:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: trip.startTime];
[components setHour: 0];
[components setMinute: 0];
[components setSecond: 0];
NSDate *newDate = [gregorian dateFromComponents: components];
但是当使用数组函数时:
[allDays indexOfObject:newDate]
我从来没有在重复的日期比赛。有谁知道如何干净地做到这一点?
最佳答案
因此,实际上我最终要做的是创建一个数组字典来存储我的对象。
字典中的每个键都是一天,对象是我的对象(行程)的数组
for(Trip *trip in allTrips)
{
NSString *dateString = [self dayString:trip.startTime];
NSMutableArray *dayArray = [dict objectForKey:dateString];
//is the day in the dictionary?
if (dayArray == nil)
{
//This day is not in the dictionary
//create an empty array
dayArray = [[NSMutableArray alloc]init];
//add the array to the dictionary
[dict setObject:dayArray forKey:dateString];
}
//add the trip to the array
[dayArray addObject:trip];
}
关于ios - 为唯一日期创建UITableView节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249825/