![](/img/trans.png)
[英]Correctly Grab the IndexPath.row of a UIButton in Custom Table Cell?
[英]Custom Table Cell Action based on indexPath.row not working?
我似乎已經將UITableView自定義單元格(正在使用單獨的XIB)正確地與我的Delete Action一起使用,正確地刪除了PROPER單元格並刷新了已刪除正確的表格視圖。
但是,當我嘗試根據單擊自定義單元格中的按鈕進行操作以獲取路線指示時,無論單擊哪個單元格,似乎都只能推送一個相同的事件,而不會更改selectedEvent對象的詳細信息。 除非我刪除單元格(使用有效的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.
}
去掉:
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];
}
導致U不想每次都對R剛創建的單元格或U通過IB執行此操作,在這種情況下U不需要此代碼行:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.