![](/img/trans.png)
[英]UITableView visibleCells is returning wrong cells after contentOffset
[英]UITableView Cell visibleCells array goes wrong?
我已經在ViewController中創建了一個UITableView
。 當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
- (NSArray *)indexPathsForVisibleRows
例:
NSArray indexArray = [self.tableView indexPathsForVisibleRows];
終於我明白了。 很擔心。 我的項目結構如下。 我在情節提要的FirstViewController的故事板的FirstViewController中有一個UITableView。 單擊單元格時。 它也推送到包含一個uitableview的SecondViewController。 而這個uitableview是我在上面問的tableview。 最后,我通過刪除情節提要和創建FirstViewController來修復它。
感謝大家。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.