首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableView visibleCells数组出错了吗?

UITableView visibleCells数组出错了吗?
EN

Stack Overflow用户
提问于 2014-04-18 02:58:31
回答 2查看 2.6K关注 0票数 1

我在一个UITableView中创建了一个ViewController。当viewdidload的时候,我注册了手机

代码语言:javascript
复制
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];

下面是UITableview 委托数据源中的代码

代码语言:javascript
复制
-(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获得的细胞总是不为零。

这是输出

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-18 04:36:29

- (NSArray *)indexPathsForVisibleRows

前:

代码语言:javascript
复制
NSArray indexArray = [self.tableView indexPathsForVisibleRows];
票数 1
EN

Stack Overflow用户

发布于 2014-04-19 17:04:43

最后我想明白了。这是相当令人忧心的。我的项目结构如下。我在故事板的FirstViewController for the FirstViewController中有一个FirstViewController。当一个细胞被点击。它推到包含uitableview的SecondViewController。这个uitableview就是我上面问的那个桌面视图。最后,我通过删除故事板和创建FirstViewController.Then来修复它,一切都很好。

谢谢大家。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23146717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档