我在一个UITableView中创建了一个ViewController。当viewdidload的时候,我注册了手机
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];下面是UITableview 委托和数据源中的代码
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 25;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"visible cell: %d",[[self.tableView visibleCells] count]);
static NSString* CellIdentifier = @"tableViewCell";
UITableViewCell* cell = cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
NSLog(@"nil -> create new");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else{
NSLog(@"not nil reuse cell success");
}
[cell.textLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"will display: %d",indexPath.row);
}结果是,当我向下滚动表视图时,会显示这些单元格的日志显示单元格。但是当我滚动到底部和顶部的时候。未显示willDisplayCell中的日志。可见细胞的数量一直增长到25个。因此,表视图似乎认为所有25个细胞仍然可见。但在屏幕上一次只显示5-6个细胞。
我想这可能是静态细胞和动态细胞的问题?我想我用的是动态的。因为从dequeueReusableCellWithIdentifier获得的细胞总是不为零。
这是输出
2014-04-18 10:51:22.101 [5834:60b] visible cell: 6
2014-04-18 10:51:22.103 [5834:60b] not nil reuse cell success
2014-04-18 10:51:22.104 [5834:60b] will display: 6
2014-04-18 10:51:22.401 [5834:60b] visible cell: 7
2014-04-18 10:51:22.403 [5834:60b] not nil reuse cell success
2014-04-18 10:51:22.405 [5834:60b] will display: 7
2014-04-18 10:51:22.767 [5834:60b] visible cell: 8
2014-04-18 10:51:22.769 [5834:60b] not nil reuse cell success
2014-04-18 10:51:22.771 [5834:60b] will display: 8
2014-04-18 10:51:23.451 [5834:60b] visible cell: 9
2014-04-18 10:51:23.453 [5834:60b] not nil reuse cell success
2014-04-18 10:51:23.455 [5834:60b] will display: 9
2014-04-18 10:51:23.551 [5834:60b] visible cell: 10
2014-04-18 10:51:23.552 [5834:60b] not nil reuse cell success
2014-04-18 10:51:23.554 [5834:60b] will display: 10
2014-04-18 10:51:23.768 [5834:60b] visible cell: 11
2014-04-18 10:51:23.769 [5834:60b] not nil reuse cell success
2014-04-18 10:51:23.772 [5834:60b] will display: 11
2014-04-18 10:51:24.067 [5834:60b] visible cell: 12
2014-04-18 10:51:24.069 [5834:60b] not nil reuse cell success发布于 2014-04-18 04:36:29
- (NSArray *)indexPathsForVisibleRows
前:
NSArray indexArray = [self.tableView indexPathsForVisibleRows];发布于 2014-04-19 17:04:43
最后我想明白了。这是相当令人忧心的。我的项目结构如下。我在故事板的FirstViewController for the FirstViewController中有一个FirstViewController。当一个细胞被点击。它推到包含uitableview的SecondViewController。这个uitableview就是我上面问的那个桌面视图。最后,我通过删除故事板和创建FirstViewController.Then来修复它,一切都很好。
谢谢大家。
https://stackoverflow.com/questions/23146717
复制相似问题