Reputation: 59
I have seem to got my UITableView Custom Cells (which are utilizing a separate XIB) to properly work with my Delete Action, properly deletes the PROPER cell and refreshes the tableview with the correct one removed.
However when I tried making an action for getting directions based on clicking the button in the custom cell, it seems to be only pushing that ONE SAME event no matter which cell is clicked not changing the selectedEvent object details. It doesn't change unless I delete the cell (with the working deleteButton), and click another. Then that one won't change until I delete that one and click another...and so on.
Any idea whats wrong? Is it because I'm setting the object details, and allocating it with that, then when they hit back it's never been deallocated or something so when they click another, details never change? Sorry new to objective c...
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.
}
Upvotes: 0
Views: 791
Reputation: 1315
remove:
PFObject *events = [self.watchingEvents objectAtIndex:indexPath.row];
self.selectedEvent = events;
from cellForRowAtIndexPath and put it in directionsButtonAction like this
-(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];
}
And this
[[cell deleteButton] addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
should be in
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WatchingCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cause U dont want to do that every time jus for the cells that R just creating or U do it through IB, in which case U dont need this line of code :)
Upvotes: 1