0

私はこの問題について何も考えていなかったので、あなた方の何人かが状況に光を当てることができるのではないかと思いました。

簡単に言うと、私は学校の生徒の時刻表をサーバーから取得して解析し、テーブルビューに表示するアプリを作成しています。各学生には1日7回の期間があり、2週間にまたがる時間割には10日があります(A週で5日、B週で5日)。

ただし、問題は、一部の学生に無料期間がある場合、つまり、その期間中にクラスがない場合にあります。サーバーは空き期間を考慮していないため、2週間で合計70の期間を返す学生もいれば、66を返す学生もいます。

ここで追いかけます...

NSDictionariesでいっぱいのNSMutableArrayを注文しました。配列内の一般的なオブジェクトは、コンソールに出力すると次のようになります。

{
    weekday = 12;
    period = 1;
    room = "Room D404";
    title = "English Extension";
},

注文のどこに切れ目があるかを把握する必要があります。その場合は、その中にオブジェクトを挿入して空白を埋めます。例を挙げましょう:

{
    dayweek = 12;
    period = 1;
    room = "Room D404";
    title = "English Extension";
},
    {
    dayweek = 12;
    period = 2;
    room = "Room W285";
    title = "Modern History";
},
    {
    dayweek = 12;
    period = 3;
    room = "Room W187";
    title = "Maths Extension 1";
},
    {
    dayweek = 12;
    period = 4;
    room = "Room W370";
    title = Economics;
},
    {
    dayweek = 12;
    period = 6;
    room = "Room D301";
    title = "English Advanced";
},
    {
    dayweek = 12;
    period = 7;
    room = "Room W282";
    title = "Studies of Religion";
},
    {
    dayweek = 13;
    period = 1;
    room = "Room W370";
    title = Economics;
},

どこにもオブジェクトがないperiod = 5ので、追加する必要があります。私が言っていることは理解しにくいですか?もしそうならごめんなさい。

前もって感謝します!

4

3 に答える 3

2

これにより、1から1日に存在する最後までの欠落期間が埋められます。与えられた最小期間まで日数を埋めることはありません。

NSMutableArray periods; // from somewhere else

NSUInteger count = [periods count];
NSUInteger currentPeriod = 0;
NSUInteger currentDayweek = 0;
for (NSUInteger i = 0; i < count; ++i)
{
    NSDictionary *periodDescription = periods[i];
    NSUInteger dayweek = [periodDescription[@"dayweek"] unsignedIntegerValue];
    if (dayweek != currentDayweek) {
        // another day
        currentPeriod = 0;
        currentDayweek = dayweek;
    }
    NSUInteger period = [periodDescription[@"period"] unsignedIntegerValue];
    currentPeriod += 1;
    while (period > currentPeriod) {
        NSDictionary *fillIn = @{ @"dayweek" : @(dayweek), @"period" : @(currentPeriod), @"room" : @"cafeteria", @"title" = "taking a break" };
        [periods insertObject:fillIn atIndex:i];
        currentPeriod += 1;
        i += 1;
        count += 1;
    }
}

最初の既存の期間の前に朝の時間を埋めたくない場合は、whileループの直前にこれを追加します。

    if (currentPeriod == 1)
        currentPeriod = period;

注:ブラウザに入力すると、エラーが発生する可能性があります。

于 2013-03-22T09:59:19.633 に答える
0

これを試して、問題が発生した場合はお知らせください

for (int i = 1; i<=self.arrayForRows.count; i++)
{
    if ([[[self.arrayForRows objectAtIndex:i]valueForKey:@"period"] intValue] == i)
    {
    }
    else
    {
        // Insert Here as [self.arrayForRows insertObject:yourObject atIndex:i];
         i=i-1;

    }
}
于 2013-03-22T09:55:27.230 に答える
0

これは部分的な回答にすぎませんが、コメントとして投稿するには長すぎます。

このスニペットの目的は、最小限のコードで欠落している期間を特定することだけです。もちろん、2週間のすべての日をループで実行する必要があります。

これを行うには、述語を使用できます。

// For each day create a list of all periods. 
// This is just a hard coded example
NSArray *allPeriodsForDay = @[@(1),@(2),@(3),@(4),@(5),@(6)];


// Get an array of the periods actually in the list
// It will look like this: @[@(1),@(2),@(4),@(5),@(6)]; // Period 3 missing
NSArray *studentsPeriods  = [periodRecords valueForKey: @"period"];

// Build a predicate to identify period(s) not in list
NSPredicate *freePredicate = [NSPredicate predicateWithFormat:@"not self in %@", studentsPeriods];

NSArray *freePeriods = [allPeriodsForDay filteredArrayUsingPredicate:freePredicate];

freePeriodsこれで、その日の学生のスケジュールの「欠落」期間のリストが含まれます。

次に、新しいものを作成NSDictionaryしてリストに追加できます。次に、すべてをperiodフィールドで並べ替えます。

于 2013-03-22T10:34:47.117 に答える