我有一个 UIViewController
,它有一个 UITableView
subview 。我想通过在单元格上向左滑动然后点击 Delete
来删除 UITableViewCells
。
然而,滑动只是偶尔被捕获。它有时工作正常,但大多数时候滑动没有被捕捉到,delete
按钮也没有出现。这是我正在使用的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES if you want the specified item to be editable.
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)table editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
// Swipe to delete.
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// perform delete logic
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Card *thisCard = self.cards[indexPath.row];
static NSString *CellIdentifier = @"CardCell";
CardCell *cell = (DCTStripeCardCell *)[DCTBase cellForTableView:tableView reuseID:CellIdentifier indexPath:indexPath];
BOOL first = indexPath.row == 0;
BOOL last = indexPath.row == ([self.cards count] - 1);
BOOL isDefault = thisCard.resourceId == self.customer.defaultCard.resourceId;
[cell update:thisCard First:first Last:last Default:isDefault];
if (isDefault) {
self.selectedCell = cell;
}
return cell;
}
这是我为 [cell update:First:Last:Default]
- (void)update:(Card *)card First:(BOOL)first Last:(BOOL)last Default:(BOOL)isDefault
{
[super updateFirst:first Last:last];
self.last4.text = [NSString stringWithFormat:@"CARD ENDING IN: %@", card.last4];
self.expiration.text = [NSString stringWithFormat:@"EXPIRES: %@/%@", card.expMonth, card.expYear];
self.cc_logo.image = [UIImage imageNamed:[card getCardImageString]];
self.defaultCardView.clipsToBounds = YES;
self.defaultCardView.layer.cornerRadius = self.defaultCardView.frame.size.height/2.0;
self.defaultCardView.layer.borderWidth = 1.0f;
if (isDefault) {
self.defaultCardView.backgroundColor = [UIColor blueColor];
}
}
我正在使用 [super update:First:Last]
舍入第一个和最后一个表格单元格,因为 ios 7 不再支持它。
代码如下:
- (void)updateFirst:(BOOL)first Last:(BOOL)last
{
self.clipsToBounds = YES;
if (first) {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners: (UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
// TODO: move common logic to another function.
if (last) {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
// This code is probably extraneous
if (!first && !last) {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners: (UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(0.0, 0.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
}
不确定我在这里遗漏了什么。我应该注意,我还实现了 delegate
方法 tableView didSelectRowAtIndexPath:
我已经在模拟器
和设备
上对此进行了测试,因此不仅仅是模拟器缺少滑动。
有什么建议吗?
最佳答案
我犯了一个愚蠢的错误——没有意识到这个特定的 UIViewController
继承自一个已经添加了 UIGestureRecognizer
的类。为了解决这个问题,我只是在父类中实现了这个函数:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
关于ios 滑动 tableviewcell 删除有时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22526072/