我想添加一個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
這是一種猜測,但不適合把它扔出去:在你的自定義單元實現你嘗試使用self.cellButton來使用屬性,而不是訪問伊娃?這有什麼區別嗎? – geraldWilliam
使用屬性而不是ivar時沒有區別 –