Cómo obtener la parte visible de UITableView
for (NSIndexPath* i in [yourTableViewName indexPathsForVisibleRows])
{
NSUInteger sectionPath = [i indexAtPosition:0];
// Aquí hay un código personalizado que se ejecutará varias veces para cada parte de cada fila visible en el grupo
}
2.
O muy fácil La forma es usar valueForKeyPath y aprovechar la clase NSSet: NSSet *visibleSections = [NSSet setWithArray:[[self.tableView indexPathsForVisibleRows] valueForKey:@"section"]];
Básicamente. Tiene una serie de valores de sección en las filas visibles y luego la completa en una colección para eliminar duplicados.
3.
He encontrado la solución. En el primer paso, se mostrará cada sección, cree una sección UIView - (UIView *)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger), que se almacenará en la matriz.
Cuando me desplazo en TableView quiero liberar la parte invisible, así que necesito saber qué parte es visible o no, mediante el código de función detecto este propósito y si la vista es visible entonces suéltela. -(BOOL)isVisibleRect:(CGRect)rect containerView:(UIScrollView*)containerView
{
CGPoint punto = contenedorView.contentOffset
CGFloat zy = punto; .y ;
CGFloat py = rect.origin.if (rect.origin.y > por) {
return FALSE;
devuelve VERDADERO;
}
(rect es parte de UIView; containerView es UITableView)
Con este método, puedo obtener la visibilidad de UITableView parte, pero me gustaría ver una manera de hacerlo usando la API proporcionada directamente por el SDK.
4. Extraiga parte de la lista de filas visibles: NSArray *indexPathsForVisibleRows = [tableView indexPathsForVisibleRows];
NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
for ( NSIndexPath *indexPath en indexPathsForVisibleRows ) {
[indexSet addIndex.indexPath.section];
}
NSLog(@"indexSet %@",indexSet) ;
// indexSet
O: 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. Otra solución es usar la etiqueta de 1 bit en la vista del encabezado de la sección de la siguiente manera #define _TBL_TAG_SECTION(_TAG) ((_TAG)|(1< <30))<. /p>
#define _TBL_TAG_CLEAR(_TAG) ((_TAG)& amp;((1<<30)-1))
#define _TBL_TAG_IS_SECTION(_TAG) ( (_TAG)>>30 )
- (UIView*)tableView: (UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// vista de encabezado de asignación
UIView *header = [[UIView alloc] initWithFrame.CGRectMake(0, 0, 1, 1)];
header.tag = _TBL_TAG_SECTION(sección); p>
return encabezado;
}
- (void)scrollViewDidScroll:( UIScrollView *)scrollView
{
CGRect r = CGRectMake(scrollView. contentOffset.x, scrollView.contentOffset.y,
CGRectGetWidth(scrollView.frame),
CGRectGetHei
ght(scrollView.frame));
for (UIView *v en [_tableView subvistas]) {
if ( CGRectIntersectsRect(r, v.frame)){
if ( _TBL_TAG_IS_SECTION(v.tag) ){
NSLog(@"etiqueta de sección visible %d",_TBL_TAG_CLEAR(v.tag));}.
}
}
}
}
6. Dos pasos para obtener la parte visible en UITableView. Solución:
1) Agregue la vista de encabezado a la matriz mutable viewForHeaderInSection 2) Actualice la propiedad scrollViewDidScroll NoteTag de la matriz para guardar el número de secciones al desplazarse por la vista de tabla
@property (no atómica, fuerte, lectura y escritura) 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 = sección;
UIView *headerView = [[UIView alloc] initWithFrame.tag = sección;
[_headerArray addObject:headerView];
devuelve headerView;
}
- (void)scrollViewDidScroll: (UIScrollView * )scrollView {
[self updateHeaderArray];
NSLog(@"----- - -----");
for (UIView *ver en _headerArray) {
NSLog(@" sección visible:%d", view.tag); p >
}
}
}
- (void)updateHeaderArray {
// Eliminar encabezados parciales invisibles p>
NSMutableArray *removeArray = [matriz NSMutableArray];
CGRect containerRect = CGRectMake(_tableView.contentOffset.x, _tableView.contentOffset.y,
_tableView.frame .size .width, _tableView.frame.size.height);
for (UIView *encabezado en _headerArray) {
if (! CGRectIntersectsRect(header.frame, containerRect)) {
[removeArray addObject:header];
}
}
[_headerArray removeObjectsInArray :removeArray];
}
7. La respuesta es mucho más simple y utiliza el conciso KVC NSArray *v.
isibleSections = [self.tableView.indexPathsForVisibleRows valueForKey:@"section"];
Esto puede proporcionarle valores de matriz duplicados, pero puede administrarlos desde allí.