2015-02-24 2 views
2

Я хочу добавить разделы и добавить строки в созданном последнем разделе.Xcode iOS добавить раздел и строку только в этом разделе

Но когда я добавляю раздел и после добавления строки он добавляет строки для всех разделов, и я хочу только в последний созданный раздел.

Я делаю это:

@interface SectionTestViewController() 
{ 
    NSMutableArray *sectionsArray; 
    NSMutableArray *rowsInSectionsArray; 
    NSInteger currentSection; 
} 
@end 

@implementation SectionTestViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.myTableView.delegate = self; 
    self.myTableView.dataSource = self; 

    sectionsArray = [[NSMutableArray alloc]init]; 
    rowsInSectionsArray = [[NSMutableArray alloc]init]; 

    currentSection = 0; 
} 

#pragma tableview delegate and datasource methods 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return sectionsArray.count; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return rowsInSectionsArray.count; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [sectionsArray objectAtIndex:section]; 
} 

акции кнопки для добавления разделов:

- (IBAction)addSectionButton:(id)sender { 
    [sectionsArray addObject:[NSString stringWithFormat:@"Section %lu", (unsigned long)sectionsArray.count]]; 

    currentSection++; 

    [self.myTableView reloadData]; 
} 

акции кнопки для добавления строк:

- (IBAction)addRowButton:(id)sender { 
    [rowsInSectionsArray addObject:[NSString stringWithFormat:@"Ligne %lu", (unsigned long)rowsInSectionsArray.count]]; 

    [self.myTableView reloadData]; 
} 

спасибо за помощь

+0

Ваш возвращающий тот же массив, 'rowsInSectionsArray', для каждого раздела в таблице. Поэтому, конечно, когда вы добавляете к нему объект, он возвращает его для каждого раздела. 'rowsInSectionsArray' должен быть массивом массивов. – random

ответ

1

Вы должны растить г сделать что-то вроде этого:

@interface SectionTestViewController() 
{ 
    NSMutableArray *sectionsArrays; 
    NSMutableArray *rowsInSectionsArray; 
    NSInteger currentSection; 
} 
@end 

@implementation SectionTestViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.myTableView.delegate = self; 
    self.myTableView.dataSource = self; 

    sectionsArray = [[NSMutableArray alloc]init]; 
    rowsInSectionsArray = [[NSMutableArray alloc]init]; 

    currentSection = 0; 
} 

#pragma tableview delegate and datasource methods 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return sectionsArray.count; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [rowsInSectionsArrays objectAtIndex:section].count; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [sectionsArray objectAtIndex:section]; 
} 

Действие кнопки для добавления разделов:

- (IBAction)addSectionButton:(id)sender { 
    [sectionsArray addObject:[NSString stringWithFormat:@"Section %lu", (unsigned long)sectionsArray.count]]; 
    [rowsInSectionsArrays addObject:[[NSMutableArray alloc] init]]; 

    currentSection++; 

    [self.myTableView reloadData]; 
} 

Действие кнопки для добавления строки в последнем разделе:

- (IBAction)addRowButton:(id)sender { 
    [[rowsInSectionsArray objectAtIndex:currentSection] addObject:[NSString stringWithFormat:@"Ligne %lu", (unsigned long)[rowsInSectionsArray objectAtIndex:currentSection].count]]; 

    [self.myTableView reloadData]; 
} 

Я просто преобразовал rowsInSectionsArray в массив массивов. Каждый массив, который он содержит, содержит строки для одного раздела.

Обязательно сделайте то же самое на tableView:cellForRowAtIndexPath:.