我只想允许滑动删除 UITableView
的第一个单元格。
这一点很简单,但是当用户尝试滑动任何其他单元格时,我想显示一个 UIAlert
。我再次通过在除单元 0 之外的每个单元上使用 UIGestureRecognizer
来完成这项工作。
我遇到的问题是,一旦删除了顶行,我希望允许删除新的顶行。
好像我需要删除我分配给单元格的 UIGestureRecognizer's
,但我不知道该怎么做。
这是我的一些代码
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 actionselector(swipeDetected];
swipeRecognizer.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
[cell addGestureRecognizer:swipeRecognizer];
}
return cell;
}
-(void)swipeDetectedUIGestureRecognizer *)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)tableViewUITableView *)tableView canEditRowAtIndexPathNSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return YES;
} else {
return NO;
}
}
-(void)tableViewUITableView *)tableView commitEditingStyleUITableViewCellEditingStyle)editingStyle forRowAtIndexPathNSIndexPath *)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 actionselector(swipeDetected];
swipeRecognizer.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
[[tableView cellForRowAtIndexPath:0]removeGestureRecognizer:swipeRecognizer];
}
}
无需移除gestureRecognizer。在您的 swipeDetected
中找出您所在的单元格,并且仅在 indexPath.row != 0
时显示警报。
您的手势识别器将为您提供可以转换为表格 View 坐标空间的位置,该坐标空间可用于获取该单元格的 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 actionselector(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;
}
关于ios - 如何从表格单元格中删除 UIGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8359833/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |