UITutton在UITableViewCell和UIView中的表现

mar*_*the 2 uibutton uitableview ios

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

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

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

演示代码:

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property UITableView* myTableView;
@property UIView* myView;
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

CustomCell.h

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

CustomCell.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
Run Code Online (Sandbox Code Playgroud)

小智 5

在表格视图中,在"滚动视图"部分下方有"延迟内容触摸"选项...将其删除并且按钮上的延迟消失但这样表滚动不会开始拖动按钮.