UITableView如何獲取所有的cell


  

 

查過文檔,官方提供了– visibleCells 方法來獲取所有可見的cell,但是僅限於獲取當前能看到的,那些需要scroll才能看到的cell獲取不到。
於是想到自己寫:

復制代碼
  1. -(NSArray *)cellsForTableView:(UITableView *)tableView
  2. {
  3.     NSInteger sections = tableView.numberOfSections;
  4.     NSMutableArray *cells = [[NSMutableArray alloc]  init];
  5.     for (int section = 0; section < sections; section++) {
  6.         NSInteger rows =  [tableView numberOfRowsInSection:section];
  7.         for (int row = 0; row < rows; row++) {
  8.             NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
  9.             [cells addObject:[tableView cellForRowAtIndexPath:indexPath]];
  10.         }
  11.     }
  12.     return cells;
  13. }



但是問題還是存在,不在屏幕范圍內的cell還是獲取不到:比如說tableview中有10個cell,滿屏可以顯示5個。當loop到第六個cell的時候,獲得是一個nil

 

 

 

 

 

 

1.UITableViews存儲他們的NSIndexPath。因此存在對部分沒有對象。使用下面的代碼就可以遍歷表並執行的可見部分索引(我不知道你為什么想要看到的部分,因為他們看到,目前在屏幕上,但不管)。 
for (NSIndexPath* i in [yourTableViewName indexPathsForVisibleRows])
{
NSUInteger sectionPath = [i indexAtPosition:0];
//custom code here, will run multiple times per section for each visible row in the group
}

2.
或者非常簡單的方法是采取valueForKeyPath和的NSSet類的優勢: NSSet *visibleSections = [NSSet setWithArray:[[self.tableView indexPathsForVisibleRows] valueForKey:@"section"]];

基本上,你在可見的行的部分值的數組,然后填入一組與此刪除重復。

3.
我已經得到了解決。 第一步,每個部分會顯示,創建一個UIView- (UIView *)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger)section,將被存儲到數組中。
當TableView中滾動,我想免費的無形剖面圖,所以我需要知道哪些部分是可見或不可見,請按功能代碼會檢測這個目的,如果視圖是可見的,然后釋放它。 -(BOOL)isVisibleRect:(CGRect)rect containerView:(UIScrollView*)containerView
{
CGPoint point = containerView.contentOffset;
CGFloat zy = point.y ;

CGFloat py = rect.origin.y + rect.size.height;
if (py - zy <0) {
return FALSE;
}
CGRect screenRect = containerView.frame;

CGFloat by = screenRect.size.height + zy ;
if (rect.origin.y > by) {
return FALSE;
}
return TRUE;
}

(rect是該部分的UIView;containerView是UITableView)
通過這種方式,我可以得到的可見部分UITableView,但我希望在SDK可以用於此目的直接提供的API。

4. 從可見的行列表中提取部分: NSArray *indexPathsForVisibleRows = [tableView indexPathsForVisibleRows];
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for ( NSIndexPath *indexPath in indexPathsForVisibleRows ) {
[indexSet addIndex:indexPath.section];
}
NSLog(@"indexSet %@",indexSet);
// indexSet <NSMutableIndexSet: 0x11a5c190>[number of indexes: 5 (in 1 ranges), indexes: (9-13)]

或: NSArray *indexPathsForVisibleRows = [detailTableView indexPathsForVisibleRows];
NSMutableSet *sectionSet = [NSMutableSet set];
for ( NSIndexPath *indexPath in indexPathsForVisibleRows ) {
[sectionSet addObject:[NSNumber numberWithInt:indexPath.section]];
}
NSLog(@"sectionSet %@",sectionSet);
// sectionSet {(13, 11, 9, 10, 12 )}

5. 另一種解決方案,可以使用在你的節頭視圖中的標簽1位,這樣的 #define _TBL_TAG_SECTION(_TAG) ((_TAG)|(1<<30))
#define _TBL_TAG_CLEAR(_TAG) ((_TAG)&((1<<30)-1))
#define _TBL_TAG_IS_SECTION(_TAG) ((_TAG)>>30)

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// alloc header view
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
header.tag = _TBL_TAG_SECTION(section);
return header;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect r = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y,
CGRectGetWidth(scrollView.frame),
CGRectGetHeight(scrollView.frame));
for (UIView *v in [_tableView subviews]) {
if ( CGRectIntersectsRect(r, v.frame) ) {
if ( _TBL_TAG_IS_SECTION(v.tag) ) {
NSLog(@"visible section tag %d", _TBL_TAG_CLEAR(v.tag));
}
}
}
}

6. 2步解決方案,以獲得在一個UITableView可見部分:
1)添加標題視圖一個可變數組viewForHeaderInSection2)更新數組時,在滾動的tableviewscrollViewDidScroll注Tag屬性來保存部分的數量
@property (nonatomic, strong, readwrite) NSMutableArray *headerArray;

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
headerView.backgroundColor = [UIColor greenColor];
headerView.tag = section;
[_headerArray addObject:headerView];
return headerView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self updateHeaderArray];
NSLog(@"------------");
for (UIView *view in _headerArray) {
NSLog(@"visible section:%d", view.tag);
}
}

- (void)updateHeaderArray {
// remove invisible section headers
NSMutableArray *removeArray = [NSMutableArray array];
CGRect containerRect = CGRectMake(_tableView.contentOffset.x, _tableView.contentOffset.y,
_tableView.frame.size.width, _tableView.frame.size.height);
for (UIView *header in _headerArray) {
if (!CGRectIntersectsRect(header.frame, containerRect)) {
[removeArray addObject:header];
}
}
[_headerArray removeObjectsInArray:removeArray];
}

7. 答案是簡單了很多,並用簡潔KVC NSArray *visibleSections = [self.tableView.indexPathsForVisibleRows valueForKey:@"section"];

這可能給你重復的值的數組,但你可以從那里管理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM