Ответ 1
ОБНОВЛЕНО: Расширяемые строки JSON - ваш проект с автоматическим скроением, работающим для всех строк.
Также проверьте Expandable Rows, я разветкил этот проект, чтобы помочь Novara и улучшить его. Теперь работаем для всех разделов, кроме вложенных строк.
ПЕРВЫЙ ЭТАП
Отслеживать открытую строку для каждого раздела. Конечно, это можно сделать, используя NSArray
tabOpened для нескольких разделов.
@interface RootViewController : UITableViewController {
NSInteger tabOpened_1;
NSInteger tabOpened_2;
}
ВТОРОЙ ЭТАП
Затем инициализируйте те, у которых значение "-1", ни одна открытая вкладка.
- (void)viewDidLoad
{
........
[self.firstForTable addObjectsFromArray:self.firstArray];
tabOpened_1 = -1;
[self.secondForTable addObjectsFromArray:self.secondArray];
tabOpened_2 = -1;
}
ТРЕТИЙ ЭТАП
Измените didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section==0) {
NSDictionary *d=[self.firstForTable objectAtIndex:indexPath.row];
if([d valueForKey:@"produtos"]) {
if (tabOpened_1 == -1) {
NSLog(@"No tab opened in first section");
tabOpened_1 = indexPath.row;
} else if(tabOpened_1 != indexPath.row && tabOpened_1 > indexPath.row){
NSDictionary *d_ = [self.firstForTable objectAtIndex:tabOpened_1];
NSArray *ar_ = [d_ valueForKey:@"produtos"];
[self miniMizeFirstsRows:ar_];
tabOpened_1 = indexPath.row;
}
NSArray *ar=[d valueForKey:@"produtos"];
BOOL isAlreadyInserted=NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[self.firstForTable indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeFirstsRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(NSDictionary *dInner in ar ) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[self.firstForTable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
}
if (tabOpened_1 == -1) {
NSLog(@"No tab opened in first section");
tabOpened_1 = indexPath.row;
} else if(tabOpened_1 != indexPath.row && tabOpened_1 < indexPath.row){
NSDictionary *d_ = [self.firstForTable objectAtIndex:tabOpened_1];
NSArray *ar_ = [d_ valueForKey:@"produtos"];
[self miniMizeFirstsRows:ar_];
tabOpened_1 = indexPath.row - [ar_ count];
tabOpened_1 = (tabOpened_1 < 0) ? indexPath.row : tabOpened_1;
}
} else {
NSLog(@"Leave Element:::%@ %@|",[d valueForKey:@"nome"],[d valueForKey:@"numeroConta"]);
}
for (int i=0; i < [[self.rowsPerSection objectAtIndex:indexPath.section] integerValue]; i++) {
NSDictionary *d_ = [self.secondForTable objectAtIndex:i];
NSArray *ar_ = [d_ valueForKey:@"produtos"];
[self miniMizeSecondsRows:ar_];
}
}
if (indexPath.section==1) {
if (tabOpened_2 == -1) {
NSLog(@"No tab opened in second section");
tabOpened_2 = indexPath.row;
} else if(tabOpened_2 != indexPath.row && tabOpened_2 > indexPath.row){
NSDictionary *d_ = [self.secondForTable objectAtIndex:tabOpened_2];
NSArray *ar_ = [d_ valueForKey:@"produtos"];
[self miniMizeSecondsRows:ar_];
tabOpened_2 = indexPath.row;
}
NSDictionary *d=[self.secondForTable objectAtIndex:indexPath.row];
if([d valueForKey:@"produtos"]) {
NSArray *ar=[d valueForKey:@"produtos"];
BOOL isAlreadyInserted=NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[self.secondForTable indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeSecondsRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(NSDictionary *dInner in ar ) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:1]];
[self.secondForTable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
}
if (tabOpened_2 == -1) {
NSLog(@"No tab opened in second section");
tabOpened_2 = indexPath.row;
} else if(tabOpened_2 != indexPath.row && tabOpened_2 < indexPath.row){
NSDictionary *d_ = [self.secondForTable objectAtIndex:tabOpened_2];
NSArray *ar_ = [d_ valueForKey:@"produtos"];
[self miniMizeSecondsRows:ar_];
tabOpened_2 = indexPath.row - [ar_ count];
tabOpened_2 = (tabOpened_2 < 0) ? indexPath.row : tabOpened_2;
}
} else {
//NSLog(@"Leave Element:::%@ %@|",[d valueForKey:@"nome"],[d valueForKey:@"book"]);
}
for (int i=0; i < [[self.rowsPerSection objectAtIndex:indexPath.section] integerValue]; i++) {
NSDictionary *d_ = [self.firstForTable objectAtIndex:i];
NSArray *ar_ = [d_ valueForKey:@"produtos"];
[self miniMizeFirstsRows:ar_];
}
}
}