Ios 为什么使用CellForRow:在Xcode 9.3中调用17次
我在Xcode 9.3上写了一个演示。模拟器是11.3。我创建了一个UITableView并实现了一个使用重用机制的代理方法。当我将单元格行高度设置为仅显示1个单元格并打印cellForRow:方法时。打印结果被调用了17次。然后我使用旧版本的Xcode运行相同的代码,但只调用了2次。这意味着Xcode 9.3并没有真正实现单元重用机制。谁能解释这个问题Ios 为什么使用CellForRow:在Xcode 9.3中调用17次,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在Xcode 9.3上写了一个演示。模拟器是11.3。我创建了一个UITableView并实现了一个使用重用机制的代理方法。当我将单元格行高度设置为仅显示1个单元格并打印cellForRow:方法时。打印结果被调用了17次。然后我使用旧版本的Xcode运行相同的代码,但只调用了2次。这意味着Xcode 9.3并没有真正实现单元重用机制。谁能解释这个问题 @interface ViewController () <UITableViewDelegate, UITableViewDataSo
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"%ld",indexPath.row);
static NSString *idf = @"idf";
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idf];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idf];
NSLog(@"create");
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 2000;
}
@end
我从一个小演示中检查这个。中频编码
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idf];
cell.contentView.backgroundColor = [UIColor cyanColor];
}
将调用tableview中的行数。但是只有包含UITableViewCellContentView和_UITableViewCellSeparatorView。我希望这能对你有所帮助。给我们看更多的代码来理解这个问题。把你的打印语句放到
if(!cell){…}
块中,检查重用是否有效。@AndreasOetjen我像你说的那样登录if(!cell){},但仍然打印了17次。如果屏幕只能显示一个单元格,我认为应该只调用两次以实现性能优化。而不是17岁times@rv7284代码已被编辑。您给出的行数为100。因此,这取决于设备的大小。无论渲染的行数是多少。将调用行方法的多个时间单元。
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idf];
cell.contentView.backgroundColor = [UIColor cyanColor];
}