2011-10-06 123 views
0

我用過濾了預覽包含Rezervacija對象的數組。 我添加了自定義按鈕「板」,並且當點擊按鈕時,我會詢問警報視圖以確認這一點,然後向服務器發送請求。當收到服務器的響應時,我需要從表中刪除行,並從filteredReservations數組中刪除對象。 當我刪除第一行沒關係,但之後在filteredReservations而不是Rezervacija對象中,我得到了UIButton對象!我不知道如何:)uitableview單元格中的自定義刪除按鈕不起作用

代碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return([filteredReservations count]); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; 
    } 
    Reservation *reservation = [filteredReservations objectAtIndex:indexPath.row]; 
    NSString *label = [[NSString alloc] initWithFormat:@"%@ (%d)", reservation.name, reservation.seatsNumber]; 
    cell.textLabel.text = label; 
    [label release]; 
    [reservation release]; 
    tableView.allowsSelection = FALSE; 
    UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [cellButton setFrame:CGRectMake(430.0, 2.0, 106.0, 40.0)]; 
    [cellButton setTitle:@"Board" forState:UIControlStateNormal]; 
    [cellButton addTarget:self action:@selector(BoardPassengers:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell addSubview:cellButton]; 
     cellButton.tag = indexPath.row; 

    return cell; 
} 


-(void)BoardPassengers:(id)sender{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Question" message:@"Do you want to board passengers?" 
                delegate:self cancelButtonTitle:@"Odustani" otherButtonTitles:@"Da", nil]; 
    [alert show]; 
    [alert release]; 
    passengerForBoarding = [NSIndexPath indexPathForRow:((UIControl*)sender).tag inSection:1]; 
} 

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    // the user clicked one of the OK/Cancel buttons 
    if (buttonIndex == 0) 
    { 
     NSLog(@"cancel"); 
    } 
    else 
    { 
     Reservation *reservationNew = [filteredReservations objectAtIndex:passengerForBoarding.row]; 
     NSString *reservationId = [[NSString alloc] initWithFormat:@"%d",reservationNew.reservationId]; 

     params = [NSDictionary dictionaryWithObjectsAndKeys: @"1", @"tour_id", @"1", @"bus_number",reservationId, @"reservation_id", nil]; 
     [[RKClient sharedClient] post:@"/service/boardingToBus.json" params:params delegate:self]; 
     [DSBezelActivityView newActivityViewForView:self.view withLabel:@"Boarding..."]; 
    } 
} 

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response { 

    if([[request resourcePath] isEqualToString:@"/service/boardingToBus.json"]){ 
     if([[response bodyAsString] isEqualToString:@"ERROR"]){ 
      [DSBezelActivityView removeViewAnimated:YES]; 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error." 
                  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     }else{ 
      [DSBezelActivityView removeViewAnimated:YES]; 

      [filteredReservations removeObjectAtIndex:passengerForBoarding.row]; 
      [self.tableView reloadData]; 

      //[self.tableView beginUpdates]; 
      //[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:passengerForBoarding] 
      //     withRowAnimation:UITableViewRowAnimationFade]; 
      // NSLog(@"%i", passengerForBoarding.row); 

      //[self.tableView endUpdates]; 

     } 
    } 

} 
+0

Rezervacija物體? – Tieme

回答

1

這聽起來像你需要確保表視圖是明確地意識到這一點,需要多少過濾保留顯示。

// assuming only one section for your table view 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return([filteredReservations count]); 
} 

欲瞭解更多信息:

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:numberOfRowsInSection

+0

對不起,我沒有提到所有代碼被剪斷的方法。我已經有了相同的方法:| – dormitkon

+0

您應該擺脫tableView的cellForRowAtIndexPath方法中的「[預訂發佈]」行。在這種情況下,保留被提取,而不是創建,因此不應該在此時釋放。這*可能與您在表格視圖中只看到按鈕的事實有關,但不能保證。 :-) –

+0

明天我會試試這個,當我得到物理設備:) – dormitkon

0

嘿,你可以得到 「預訂ID」 的值,通過這一點。

-(void)BoardPassengers:(id)sender{ 
    Reservation *reservationNew = [filteredReservations objectAtIndex:[sender tag]]; 
    NSString *reservationId = [NSString StringWithFormat:@"%d",reservationNew.reservationId];} 

或者你可以在任何你想要的地方使用這個值,就像你通過提示框提到的那樣。

相關問題