ios - 为唯一日期创建UITableView节

标签 ios objective-c uitableview nsdate

我想为每个唯一的日期在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/

相关文章:

ios - native 链接失败,重复符号 : '_main' while adding native Binding Dll and Xamarin. Firebase.iOS.Database

iOS map View - 缩放以固定然后激活标注

ios - 将 iOS 应用程序从非 ARC 迁移到 ARC 的截止日期

ios - 来自 NSURLSession 的 performSegueWithIdentifier

ios - UITableViewCell 中的 UIButton 是可点击的(获取正确的事件)但问题是点击效果不可见

ios - 使用 UIAppearance 设置 UITableView 的背景颜色

objective-c - 是否可以在 Sprite Kit 中使用圆形 (SKShapeNode) 作为 mask ?

ios - 带分页的UITableView,每个cell是一个UIScrollView

objective-c - NSToolbarItem 选择不覆盖文本

ios - 使用 plist 在 TableView 之间传递数据