ios - UITableViewCell 与 UIView 中的 UIButton 性能对比

标签 ios uitableview uibutton

我想将 UIButton 添加到自定义 UITableViewCell (以编程方式)。这很容易做到,但我发现单元格中按钮的“性能”很慢 - 也就是说,当我触摸按钮时,有相当多的延迟,直到按钮在视觉上进入突出显示状态。相比之下,常规 UIView 上相同类型的按钮响应速度非常快。

为了隔离问题,我创建了两个 View - 一个是简单的 UIView,另一个是只有一个 UITableViewCell 的 UITableView。我已向两个 View (UIView 和 UITableViewCell)添加了按钮,性能差异非常惊人。

我已经在网上搜索并阅读了Apple文档,但还没有真正找到问题的原因。我的猜测是,它在某种程度上与响应者链有关,但我不能完全确定它。我一定做错了什么,我会很感激任何帮助。谢谢。

演示代码:

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property UITableView* myTableView;
@property UIView* myView;

ViewController.m

#import "ViewController.h"
#import "CustomCell.h"

@implementation ViewController

@synthesize myTableView, myView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self initMyView];
        [self initMyTableView];
    }
    return self;
}

- (void) initMyView {
    UIView* newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width,100)];
    self.myView = newView;
    // button on regularView
    UIButton* myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [myButton addTarget:self action:@selector(pressedMyButton) forControlEvents:UIControlEventTouchUpInside];
    [myButton setTitle:@"I'm fast" forState:UIControlStateNormal];
    [myButton setFrame:CGRectMake(20.0, 10.0, 160.0, 30.0)];
    [[self myView] addSubview:myButton];
}

- (void) initMyTableView {
    UITableView *newTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,100,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height-100) style:UITableViewStyleGrouped];
    self.myTableView = newTableView;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
}

-(void) pressedMyButton {
    NSLog(@"pressedMyButton");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self view] addSubview:self.myView];
    [[self view] addSubview:self.myTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    if (customCell == nil) {
       customCell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CustomCell"];
    }
    return customCell;
}

@end

CustomCell.h

#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell
@property (retain, nonatomic) UIButton* cellButton;
@end

自定义单元格.m

#import "CustomCell.h"

@implementation CustomCell

@synthesize cellButton;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // button within cell
        cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [cellButton addTarget:self action:@selector(pressedCellButton) forControlEvents:UIControlEventTouchUpInside];
        [cellButton setTitle:@"I'm sluggish" forState:UIControlStateNormal];
        [cellButton setFrame:CGRectMake(20.0, 10.0, 160.0, 30.0)];
        [self addSubview:cellButton];
    }
    return self;
}

- (void) pressedCellButton {
    NSLog(@"pressedCellButton");
}

@end

最佳答案

在表格 View 上,“ ScrollView ”部分下有一个选项“延迟内容触摸”...删除它,按钮上的延迟就消失了,但这样表格滚动就不会开始拖动按钮。

关于ios - UITableViewCell 与 UIView 中的 UIButton 性能对比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810803/

相关文章:

ios - 带有上标的NSLocalizedString

ios - Swift 中高斯图像金字塔的下采样和上采样

iphone - UITableViewController 内的 UIButton

ios - 选择行时 TableViewController 中的两个动画

ios - 为什么 Xcode 4 没有对我不完整的 UITableViewDataSource 协议(protocol)实现发出警告?

ios - 动态生成按钮时应用程序移动缓慢

ios - 通过浮点值将按钮图像从一个图像淡化到另一个图像

ios - clang : error: linker command failed with exit code 1 (use -v to see invocation) in Xcode 10. 0 和 Ionic 3

ios - 观察 Firebase 的调用在函数退出后未更新字典的值

ios - UIButton 处于循环中,事件仅在第一个元素上触发