簡體   English   中英

在UITableView部分中對自定義對象進行排序

[英]Sorting custom objects in UITableView section

我正在創建一個應用程序,而我創建了一個繼承自NSObject的自定義類。 此類包含各種屬性,其中一個是NSDate屬性,另一個是NSString。 我想使用部分在UITableView中對這些對象進行排序,而每個部分表示對象中NSDate的月份和年份。 到目前為止,我只是設法使用詳細的UITableViewCell填充列表來顯示信息,但我不知道如何使用部分,因為它們是動態的而不是靜態單元格。

我有一些頭腦風暴的解決方案,其中一個就是為所有對象創建一個for循環並計算月數並在numberOfSectionsInTableView:方法中返回該數字 - 但是如果這是最好的並且我很難確定最恰當的解決方法。

有人可以幫我嗎?

我的自定義對象包含各種屬性,但這些是我們需要關注的:

@property (nonatomic, strong) NSString *information;
@property (nonatomic, strong) NSDate *dateAdded;

謝謝! 埃里克

所以給你的對象一年和一個屬性。 在屬性的getter中,檢查實例變量。 如果是!0,請將其退回。 如果== 0,使用NSCalendar計算年份和月份,將其轉換為數字(年* 100 +月),將其保存在ivar中,然后返回。

(還要使date屬性上的setter歸零yearAndMonth屬性。)

現在,您可以根據此屬性的值編寫將表視圖分成幾部分的代碼。 對於任何給定的對象,它只會被計算一次,因此它不應該影響性能。

我認為很多都是偏好問題。 在過去這樣的事情,我只會創建一個兩個demential數組。 如果數組中的數組表示該部分,並且您需要一個部分標題,則將該部分數組拉出並根據該數組中的第一個對象填充標題。

NSArray *sectionArray = [self arrayAfterSorting:customObjectArray];

//section would be something like this
NSArray *section = sectionArray[section];
return section.count;

//header
NSArray *section = sectionArray[section];
CustomObject *customObject = section[0];
return customObject.whateverHeaderShouldBeBasedOnObject;

我個人試圖避免使用兩個獨立的數組,而是希望將所有內容都集成到一個二維數組中。 希望有意義和有幫助。

編輯:

如果問題是如何排序我會看看這個問題和答案。 雖然他們正在談論將它放入字典中,但我相信你可以將相同的邏輯應用於數組。

按月將NSArray與NSDate對象分類到NSDictionary中

希望有所幫助。

是您的數組,其中包含您的自定義對象排序? 可能不是我猜。 我首先從那開始:

NSArray *sortedObjectArray = [youArray sortedArrayUsingComparator:^NSComparisonResult(YourObjectClass *a, YourObjectClass *b) {
return [a.dateAdded compare:b.dateAdded];}];

正如您現在所了解的那樣,最好找出您需要多少個表視圖部分。 循環使用sortedArray將是最好的方法。 但請記住,例如在viewDidLoad或其他您檢索數據的地方,以確保您實際只執行一次搜索和數據聚合。 在numberOfSectionsInTableView中執行此操作會導致您的應用在每次重新加載tableView時重復所有這些不必要的計算。

那么,這些部分呢? 我推薦一個NSMutableArray來代表你需要的部分。 然后,此對象應包含NSMutableArrays本身,其中包含您的自定義對象。

但首先我們需要找出你真正需要多少部分。只需像這樣做一個for循環:

NSMutableArray *dateArray = [[NSMutableArray alloc] init];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDate *recentDate;
for (YourObjectClass *object in sortedObjectArray) {
    if (!recentDate) {
       [dateArray addObject:object.dateAdded];
       recentDate = object.dateAdded;
    }
    else {
       NSDateComponents *currentDateComponents = [calendar components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:object.dateAdded];
       NSDateComponents *recentDateComponents = [calendar components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:recentDate];
       if (recentDateComponents.year != currentDateComponents.year || recentDateComponents.month != currentDateComponents.month) {
         [dateArray addObject:object.dateAdded];
         recentDate = object.dateAdded;
       } 
    }

}

所以現在你得到了dateArray,其中包含月和年的所有不同日期。

NSMutableArray *finalArray = [[NSMutableArray alloc] init];
for (NSDate *date in dateArray) {
    NSMutableArray *subArray = [[NSMutableArray alloc] init];
    for (YourObjectClass *object in sortedObjectArray) {
        NSDateComponents *currentDateComponents = [calendar components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:object.dateAdded];
        NSDateComponents *recentDateComponents = [calendar components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
        if (recentDateComponents.year == currentDateComponents.year || recentDateComponents.month == currentDateComponents.month) {
           [subArray addObject:object];
        } 
        else {
            break;
        }
    }
    [finalArray addObject:subArray];
}

這應該是它。 現在告訴你的tableView數據源你有finalArray.count部分。 在numberOfRowsInSection中:您只需在sectionIndex上獲取subArray並返回其計數值。 希望這可以幫助你:)(沒有測試代碼,所以要注意:))

我設法將這些代碼放在一起工作,雖然我意識到我可能想要更改UI,因為數據沒有像我最初想的那樣呈現。 這是我制作的代碼,所有其他答案都幫助了我!

    // Create a sections NSMutableArray
_sectionsArray = [[NSMutableArray alloc] init];

// Cycle through the workdays and populate sectionsArray
NSString *currentMonth;
NSMutableArray *currentMonthArray = [[NSMutableArray alloc] init];
for (DayModel *currentModel in sortedWorkDays)
{
    // Create a currentMonthArray and initialize it

    // Initial currentMonth value
    if (currentMonth == nil)
    {
        currentMonth = [self monthFromDate:currentModel.registerDate];
    }

    if ([currentMonth isEqualToString:[self monthFromDate:currentModel.registerDate]])
    {
        NSLog(@"current");
        // Current month
        [currentMonthArray addObject:currentModel];

        if (([sortedWorkDays indexOfObject:currentModel] + 1) == [sortedWorkDays count])
        {
            // Complete
            NSLog(@"LAST OF ALL");
            [_sectionsArray addObject:currentMonthArray];
            currentMonthArray = nil;
            currentMonthArray = [[NSMutableArray alloc] init];
            currentMonth = [self monthFromDate:currentModel.registerDate];
            [currentMonthArray addObject:currentModel];
        }
    } else
    {
        // Finished with this month
        NSLog(@"LAST");
        [_sectionsArray addObject:currentMonthArray];
        currentMonthArray = nil;
        currentMonthArray = [[NSMutableArray alloc] init];
        currentMonth = [self monthFromDate:currentModel.registerDate];
        [currentMonthArray addObject:currentModel];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM