我想只允许在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];
}
}
答案 0 :(得分:1)
无需删除gestureRecognizer。在swipeDetected
中找出您所在的单元格,并仅在indexPath.row != 0
时显示提醒。
您的手势识别器会为您提供可以转换为表格视图坐标空间的位置,该位置可用于获取该单元格的indexPath。
在swipeDetected
CGPoint location = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
if ( indexPath.row != 0 {
// do alert
}
使用示例代码进行更新:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Test"];
if ( !cell ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"] autorelease];
UISwipeGestureRecognizer *gr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleCellSwipe:)];
gr.direction = UISwipeGestureRecognizerDirectionRight + UISwipeGestureRecognizerDirectionLeft;
gr.delegate = self;
[cell addGestureRecognizer:gr];
[gr release];
}
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d",indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( indexPath.row == 0 )
return UITableViewCellEditingStyleDelete;
else
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row == 0;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// do stuff
}
- (void)handleCellSwipe:(UIGestureRecognizer *)gestureRecognizer
{
if ( gestureRecognizer.state == UIGestureRecognizerStateRecognized ) {
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
if ( indexPath.row != 0 ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cell Swipe"
message:@"Cell in row not equal to 0 swiped"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
return indexPath.row != 0;
}
答案 1 :(得分:1)
当你可以通过caneditrowatindexpath显示警报时,为什么要经历手势识别器的麻烦。我唯一能看到的问题是你是否也使用了编辑按钮 - 那么你会立刻收到很多警报。
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return YES;
} else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:@"You can only delete starting from the top cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
如果您使用手势识别器设置,则只需使用reloadRowsAtIndexPaths:withRowAnimation:在删除后的索引0上将重置第一个单元格。但是你的cellforrowatindexpath在原帖中设置错误,无论如何都会导致这种设置发生奇怪的事情。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BetCell"];
if ( !cell ) //this is needed in case a cell was never created in the first place
cell = [[[BetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BetCell"] autorelease];
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];
} else {//this is needed to reset a cell that is being reused (such as when you scroll off the page and return)
for (UIGestureRecognizer *gesture in cell.gestureRecognizers) {
[cell removeGestureRecognizer:gesture];
}
}
return cell;
}