繁体   English   中英

UITableViewCell与UIView中的UIButton性能

[英]UIButton performance in UITableViewCell vs UIView

我想将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;

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

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

在表格视图的“滚动视图”部分下,有“延迟内容触摸”选项...将其删除,按钮上的延迟消失了,但是表格滚动不会开始拖动按钮。

我认为这与您的工作无关(我对其进行了测试,但速度有些慢,但我不会称其为“缓慢”)。 它可能与附加到表格视图的各种手势识别器有关-操作系统必须弄清楚正在发生什么手势,这可能会导致轻微的延迟。 这是tableView.gestureRecognizers的日志:

2012-10-09 20:34:12.355 SlowButtonsInTableView[3635:c07] (
    "<UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x71b42b0; state = Possible; delaysTouchesBegan = YES; view = <UITableView 0x789f800>; target= <(action=delayed:, target=<UITableView 0x789f800>)>>",
    "<UIScrollViewPanGestureRecognizer: 0x71b4940; state = Possible; delaysTouchesEnded = NO; view = <UITableView 0x789f800>; target= <(action=handlePan:, target=<UITableView 0x789f800>)>>",
    "<UISwipeGestureRecognizer: 0x71b4e00; state = Possible; view = <UITableView 0x789f800>; target= <(action=handleSwipe:, target=<UITableView 0x789f800>)>; direction = right,left>",
    "<UIGobblerGestureRecognizer: 0x71b5100; state = Possible; enabled = NO; view = <UITableView 0x789f800>>"
)

如果未启用在表格视图上滚动,则延迟会完全消失,这可能是由于滚动所需的手势引起的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM