Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone uitableview单元格中的自定义删除按钮don';行不通_Iphone_Objective C_Ios_Uitableview - Fatal编程技术网

Iphone uitableview单元格中的自定义删除按钮don';行不通

Iphone uitableview单元格中的自定义删除按钮don';行不通,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我使用包含Rezervacija对象的filteredReservations数组创建了tableView。 我添加了自定义按钮“board”,当按钮被点击时,我用alert视图请求确认,然后将请求发送到服务器。当收到服务器的响应时,我需要从表中删除行,从filteredReservations数组中删除对象。 当我删除第一行时没问题,但是在filteredReservations中,而不是在Rezervacija对象中,我得到了UIButton对象!我不知道怎么做:) 代码: - (NSInt

我使用包含Rezervacija对象的filteredReservations数组创建了tableView。 我添加了自定义按钮“board”,当按钮被点击时,我用alert视图请求确认,然后将请求发送到服务器。当收到服务器的响应时,我需要从表中删除行,从filteredReservations数组中删除对象。 当我删除第一行时没问题,但是在filteredReservations中,而不是在Rezervacija对象中,我得到了UIButton对象!我不知道怎么做:)

代码:

- (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];

        }
    }

} 

听起来您需要确保表视图明确知道需要显示多少过滤保留

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


嘿,你可以通过这个获得“预订ID”的值


或者您可以在任何地方使用此值,就像您通过“警报框”提到的那样。

对不起,我没有提到“代码剪切”中的所有方法。我已经有了同样的方法:|您应该去掉tableView的CellForRowatineXpath方法中写着“[预订释放]”的行。在这种情况下,保留是获取的,而不是创建的,因此不应在此时释放。这可能与您在表视图中只看到按钮有关,但不能保证这一点。:-)我将在明天获得物理设备时尝试此功能:)
-(void)BoardPassengers:(id)sender{
    Reservation *reservationNew = [filteredReservations objectAtIndex:[sender tag]];
    NSString *reservationId = [NSString StringWithFormat:@"%d",reservationNew.reservationId];}