2012-10-10 59 views
2

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

为了找出问题,我创建了两个视图 - 一个是简单的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 
+0

这是一种猜测,但不适合把它扔出去:在你的自定义单元实现你尝试使用self.cellButton来使用属性,而不是访问伊娃?这有什么区别吗? – geraldWilliam

+0

使用属性而不是ivar时没有区别 –

回答

0

我不认为它与你在做什么有关(我测试了它,它有点慢,但我不会称之为“呆滞”)。它可能与连接到桌面视图的各种手势识别器有关 - 操作系统必须找出正在发生的手势,这可能会造成轻微的延迟。这是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>>" 
) 
+0

谢谢 - 我不知道tableView.gestureRecognizers,这是一个很好的领导。鉴于其中一些是私人的(即UIGobblerGestureRecognizer),可能没有多少人能够做到。我同意你的看法,“呆滞”可能太强大了,但它足够让人困扰 - 可能会远离突出显示按钮的位置。再次感谢! –

-1

随着滚动的表视图没有启用延迟完全消失,大概延迟被变更手势滚动

5

在表视图造成的,根据第“滚动视图”有“延迟内容接触”选项...删除它,按钮上的延迟消失,但在这种方式表滚动不开始拖动按钮。

+0

+1。这是对这个问题的真实答案。我正在寻找'拖延内容触动',但我有'tableviewcell'选择而不是整个表在IB:D –