ios - UITableView失去当前选择,而手指临时触摸另一行,仅在iPad上

标签 ios objective-c ipad uitableview selection

在UITableView中选择单元格似乎在iPad和iPhone上工作不同。

在iPhone上,我看到以下行为:

  • 轻敲一行并抬起手指
  • 现在选择了该行
  • 点按另一行,但不要抬起手指
  • 现在将突出显示新行(看起来像已选中)
  • 旧行保持选中状态
  • 移动手指以滚动(上一步中手指仍在iPhone上)
  • 新行失去其突出显示的
  • 旧行保持选中状态
  • 抬起手指
  • 旧行保持选中状态

  • 但是在iPad上,我观察到了以下行为:
  • 轻敲一行并抬起手指
  • 现在选择了该行
  • 点按另一行,但不要抬起手指
  • 现在将突出显示新行(看起来像已选中)
  • 旧行丢失了选择
  • 移动手指以滚动(上一步中手指仍在iPad上)
  • 新行失去其突出显示的
  • 旧行仍缺少其选择
  • 抬起手指
  • 再次选择旧行

  • 使UITableView在iPad上像在iPhone上一样工作的最干净的方法是什么?

    这是重现该问题的最小应用程序。它基于Xcode 5的“空应用程序”模板。它同时在iOS 7和6.1上发生。

    ViewController.h
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UITableViewController
    
    @end
    

    ViewController.m
    #import "ViewController.h"
    
    @implementation ViewController
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 42;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        cell.textLabel.text = [@(indexPath.row) stringValue];
    
        return cell;
    }
    
    @end
    

    AppDelegate.m
    #import "AppDelegate.h"
    #import "ViewController.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
    
        ViewController *viewController = [[ViewController alloc] init];
        self.window.rootViewController = viewController;
    
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    @end
    

    最佳答案

    这是因为UITableViewtouchesBegan期间取消选择了选定的行。这似乎只发生在iPad上,而不发生在iPhone上。不知道为什么会有区别。

    为了使iPad版本的行为类似于iPhone版本,我最终对UITableView进行了子类化以覆盖此行为。我希望对这个问题有更好的答案,但这是我的解决方案:

    @interface CustomTableView : UITableView  
    
    @property (nonatomic) BOOL inTouchSequence;    
    
    @end
    
    @implementation CustomTableView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        self.inTouchSequence = YES;
        [super touchesBegan:touches withEvent:event];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        self.inTouchSequence = NO;
        [super touchesEnded:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        self.inTouchSequence = NO;
        [super touchesCancelled:touches withEvent:event];
    }
    
    @end
    

    然后在我的自定义单元格中,添加了对tableView的引用,并覆盖了setSelected方法,以防止touchesBegan取消选择单元格。
    @interface CustomTableViewCell : UITableViewCell
    
    @property (weak, nonatomic) CustomTableView* tableView;
    
    @end
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        if (self.tableView.inTouchSequence) {
            // Skip deselection while tableview is being touched.
            return;
        }
    
        [super setSelected:selected animated:animated];
    
        // Custom selection logic
    }
    

    关于ios - UITableView失去当前选择,而手指临时触摸另一行,仅在iPad上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460892/

    相关文章:

    iphone - 自己的健康指标出现问题。 Cocos2d

    ios - 使用iOS的Tesseract OCR逐字扫描图像

    iphone - CollectionView类View,可水平+垂直滚动

    ios - 无法使用 Facebook iOS SDK 进行 native 登录

    ios - CALayer 不会针对不同的方向使用渐变颜色自动调整大小

    ios - Cocos-2d iOs-cclayer的不同内存释放行为

    iphone - 如何在 iPhone 3.1.3 模拟器上运行通用应用程序?

    iphone - Xcode with phonegap project Ipad模拟器变成iphone大小

    ios - 用户是否看到 iOS 应用程序中使用的 bundle ID?

    iphone - 是否可以确定 UIImage 是否可拉伸(stretch)?