我似乎让我的 UITableView 自定义单元格(使用单独的 XIB)可以正确使用我的删除操作,正确删除 PROPER 单元格并刷新表视图并删除正确的单元格。
但是,当我尝试通过单击自定义单元格中的按钮来执行获取方向的操作时,无论单击哪个单元格,它似乎都只会推送该 SAME 事件,而不会更改 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.
}
删除:
PFObject *events = [self.watchingEvents objectAtIndex:indexPath.row];
self.selectedEvent = events;
从cellForRowAtIndexPath并将其放在方向按钮像这样操作
-(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];
}
因为 U 不想每次都为 R 刚刚创建的单元格或 U 通过 IB 创建的单元格执行此操作时都这样做,在这种情况下,U 不需要这行代码:)