【发布时间】:2011-12-02 16:58:20
【问题描述】:
我只想允许在UITableView 的第一个单元格上滑动删除。
这一点很简单,但是当用户尝试滑动任何其他单元格时,我想显示UIAlert。我再次通过在除单元 0 之外的每个单元上使用 UIGestureRecognizer 来实现此功能。
我遇到的问题是,一旦删除了顶行,我希望允许删除新的顶行。
好像我需要删除分配给单元格的UIGestureRecognizer's,但我不知道该怎么做。
这是我的一些代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BetCell"];
Bet *bet = [self.bets objectAtIndex:([self.bets count]-indexPath.row-1)];
cell.BFNeedLabel.text = bet.BFNeeded;
if (indexPath.row != 0) {
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDetected:)];
swipeRecognizer.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
[cell addGestureRecognizer:swipeRecognizer];
}
return cell;
}
-(void)swipeDetected:(UIGestureRecognizer *)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:@"You can only delete starting from the top cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return YES;
} else {
return NO;
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the very first row
if ((editingStyle == UITableViewCellEditingStyleDelete) && (indexPath.row == 0)) {
Bet *betObj = [self.bets objectAtIndex:([self.bets count]-indexPath.row-1)];
//Delete from array
[self.bets removeObjectAtIndex:([self.bets count]-indexPath.row-1)];
//Delete the row
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//Attempt to remove gesture recognizer from cell 0
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDetected:)];
swipeRecognizer.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
[[tableView cellForRowAtIndexPath:0]removeGestureRecognizer:swipeRecognizer];
}
}
【问题讨论】:
标签: ios uitableview xcode4.2 uigesturerecognizer