0

我似乎让我的 UITableView 自定义单元格(使用单独的 XIB)与我的删除操作一起正常工作,正确删除了正确的单元格并在删除正确的单元格后刷新了 tableview。

但是,当我尝试通过单击自定义单元格中的按钮来获取方向时,似乎只是推送了一个相同的事件,无论单击哪个单元格,都不会更改 selectedEvent 对象的详细信息。除非我删除单元格(使用有效的 deleteButton)并单击另一个单元格,否则它不会改变。然后那个不会改变,直到我删除那个并单击另一个......等等。

知道有什么问题吗?是因为我正在设置对象详细信息,并用它来分配它,然后当他们回击它时它从未被释放或其他什么,所以当他们单击另一个时,细节永远不会改变?对不起,目标c的新手...

表格视图.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
        // Configure the cell...
    WatchingCustomCell *cell = (WatchingCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WatchingCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    PFObject *events = [self.watchingEvents objectAtIndex:indexPath.row];
    self.selectedEvent = events;


    cell.deleteButton.tag = indexPath.row;
    [[cell deleteButton] addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    //...
    cell.directionsButton.tag = indexPath.row;
    [[cell directionsButton] addTarget:self action:@selector(directionsButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}
-(IBAction) deleteButtonAction:(id) sender
{
[SVProgressHUD showWithStatus:@"Removing from Watch List..."];

PFRelation *relation = [self.currentUser relationforKey:@"watching"];
[relation removeObject:self.selectedEvent];
[self.watchingEvents removeObject:self.selectedEvent];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (error)
    {
        NSString *errorString = [[error userInfo] objectForKey:@"error"];
        UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlertView show];
    }
    else
    {
        [SVProgressHUD showSuccessWithStatus:@"Removed from Watch List!"];
        [self refreshTableView];
    }
}];
}

-(IBAction) directionsButtonAction:(id) sender
{
    DirectionsViewController *viewController = [[DirectionsViewController alloc] init];
    if (viewController != nil)
    {
        viewController.selectedEvent = self.selectedEvent;
    }
    [self presentViewController:viewController animated:YES completion:nil];
}

DirectionsViewController.m

- (void)viewDidLoad
{
  [super viewDidLoad];

   NSLog (@"%@", self.selectedEvent);
  // This isn't changing for some reason, unless the first cell that was selected was deleted, then another is clicked, and the chain continues. 

}
4

1 回答 1

1

消除:

    PFObject *events = [self.watchingEvents objectAtIndex:indexPath.row];
        self.selectedEvent = events;

从 cellForRowAtIndexPath 并把它放在像这样的方向ButtonAction

-(IBAction) directionsButtonAction:(id) sender
{
    DirectionsViewController *viewController = [[DirectionsViewController alloc] init];
    if (viewController != nil)
    {
        viewController.selectedEvent = [self.watchingEvents objectAtIndex:[sender tag]];
    }
    [self presentViewController:viewController animated:YES completion:nil];
}

还有这个

 [[cell deleteButton] addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside];

应该在

 if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WatchingCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

因为你不想每次都为 R 刚刚创建的单元格或你通过 IB 做的单元格这样做,在这种情况下你不需要这行代码:)

于 2013-11-14T14:39:43.400 回答