基于indexPath.row的自定义表格单元格操作不起作用?

时间:2013-11-14 14:22:17

标签: ios objective-c uitableview

我似乎得到了我的UITableView自定义单元格(使用单独的XIB)来正确使用我的删除操作,正确删除了PROPER单元格并刷新了正确删除的表格视图。

然而,当我尝试基于单击自定义单元格中的按钮来获取方向的操作时,无论单击哪个单元格而不更改selectedEvent对象详细信息,它似乎只推送一个SAME事件。除非我删除单元格(使用工作deleteButton),否则它不会更改,然后单击另一个单元格。然后,那个将不会改变,直到我删除那个并单击另一个......依此类推。

任何想法都错了吗?是因为我正在设置对象详细信息,并将其分配给它,然后当它们回击它时,它从未被释放,或者当它们点击另一个时,某些东西是如此,细节永远不会改变?对不起,客观新闻......

Tableview.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. 

}

1 个答案:

答案 0 :(得分:1)

删除:

    PFObject *events = [self.watchingEvents objectAtIndex:indexPath.row];
        self.selectedEvent = events;
来自cellForRowAtIndexPath的

并将其置于如此

的directionsButtonAction中
-(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执行此操作,在这种情况下U不需要这行代码:)