uitableview单元格中的自定义删除按钮不起作用
问题描述:
我用过滤了预览包含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];
}
}
}
答
这听起来像你需要确保表视图是明确地意识到这一点,需要多少过滤保留显示。
// assuming only one section for your table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return([filteredReservations count]);
}
欲了解更多信息:
答
嘿,你可以得到 “预订ID” 的值,通过这一点。
-(void)BoardPassengers:(id)sender{
Reservation *reservationNew = [filteredReservations objectAtIndex:[sender tag]];
NSString *reservationId = [NSString StringWithFormat:@"%d",reservationNew.reservationId];}
或者你可以在任何你想要的地方使用这个值,就像你通过提示框提到的那样。
Rezervacija物体? – Tieme