3

33行のUITableViewがあります。各行は、1日の特定の時間枠を表します。テーブルビューを保持するビューが読み込まれると、それに応じて各行にデータを入力する必要があります。

ビューに渡される予約オブジェクトの配列があります。各予約には、スロット番号、予約名、および予約期間がスロットに含まれています。

テーブルにデータを入力するための最良の方法は何ですか。現在、- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathメソッド内の予約の配列を反復処理しています。

これは私に期待する結果や振る舞いを与えてくれません。ループを繰り返し処理し続けるため、パフォーマンスが大幅に低下し、スクロール後に青色であってはならないセルが青色になります。これに取り組むための最良の方法は何ですか?以下のコードを含めました。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 33;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *timeStamp = [NSString stringWithFormat:@"%.2f", (indexPath.row + 14.0 ) / 2.0];
    timeStamp = [timeStamp stringByReplacingOccurrencesOfString:@".50" withString:@":30"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@: ", timeStamp];

    for (Reservation *temp in bookingsArray) {
        if ((temp.slotNumber - 1) == indexPath.row) {
            cell.textLabel.text = [NSString stringWithFormat:@"%@: %@", timeStamp, temp.reservationName];
            cell.contentView.backgroundColor = [UIColor blueColor];
        }
        for (NSNumber *tempNo in temp.slotIdentifiers) {
            if ([tempNo intValue] -1 == indexPath.row) {
                //cell.textLabel.text = [NSString stringWithFormat:@"%@: Booked", timeStamp];
                cell.contentView.backgroundColor = [UIColor blueColor];
            }
        }
    }

    return cell;
}

更新次のことを試してみると、スクロールを開始した後にすべてのセルが青色に変わるという奇妙な動作が発生します。

- (void)viewDidLoad
{
    [super viewDidLoad];
    bookManager = appDelegate.bookingManager;
    bookingsArray = [[NSArray alloc] initWithArray:[bookManager getBookingsForCourt:1 onDate:[NSDate date]]];
    namesArray = [[NSMutableDictionary alloc] init];
    slotIndexSet = [NSMutableIndexSet indexSet];

    for (int c = 0; c < 33; c++) {
        [namesArray setObject:@"Available" forKey:[NSNumber numberWithInt:c]];
    }

    for (Reservation *temp in bookingsArray) {
        [namesArray setObject:temp.reservationName forKey:[NSNumber numberWithInt:temp.slotNumber]]; 
        for (NSNumber *slotNo in temp.slotIdentifiers) {
            [slotIndexSet addIndex:[slotNo intValue] + 1];
        }
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *timeStamp = [NSString stringWithFormat:@"%.2f", (indexPath.row + 14.0 ) / 2.0];
    timeStamp = [timeStamp stringByReplacingOccurrencesOfString:@".50" withString:@":30"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@: ", timeStamp];
    cell.textLabel.text = [namesArray objectForKey:[NSNumber numberWithInt:indexPath.row]]; 

    if ([slotIndexSet containsIndex:indexPath.row]) {
       cell.contentView.backgroundColor = [UIColor blueColor]; 
    }

    return cell;
}
4

1 に答える 1

1

これを高速化するには、次の2つのことを行う必要があります。

  • インデックスのオブジェクトがに等しくなるように配列に変換bookingsArrayします。あなたはそれを受け取ったときに元の予約配列を繰り返すことによってそれを行うことができます。bookingBySlotNumberislotNumber - 1i
  • 予約されたアイテムのインデックスを含むNSIndexSet呼び出されたものを作成します。すべてを確認し、予約済みのアイテムのインデックスにマークを付けるisBookedBySlotNumberことで、準備できます。Reservation.slotIdentifiersisBookedBySlotNumber

これらの2つの前処理されたアイテムを配置すると、ネストされたループを完全に排除できます。外側のループはのルックアップに置き換えられ、内側のループはのループアップに置き換えられbookingBySlotNumberますisBookedBySlotNumber

于 2012-08-30T21:00:22.987 に答える