簡體   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