こんにちは、拡張可能なテーブルビューで複数の配列を適用しようとしました。
しかし、コードを実行すると例外が表示されます。
例外:'無効な更新: セクション 1 の行数が無効です。更新後の既存のセクションに含まれる行数 (4) は、更新前にそのセクションに含まれる行数 (0) に等しい必要があります。プラスまたはそのセクションから挿入または削除された行の数を引いたもの (0 挿入、0 削除) およびプラスまたはマイナスそのセクションに移動した行数 (0 移動イン、0 移動アウト)
この例外を解決する方法 (または) 他の例が利用可能である場合は、私に提案してください。
私を助けてください。
私のコード:
.m ファイル
#import "ExpandableListviewWithServices.h"
@interface ExpandableListviewWithServices (){
BackGroundPostServiceClass3 * back;
NSMutableArray *arrayForBool,*totalListOfArray,*respArray;
NSArray *sectionTitleArray;
UITableView *tableList;
}
@end
@implementation ExpandableListviewWithServices
- (void)viewDidLoad {
[super viewDidLoad];
tableList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
tableList.translatesAutoresizingMaskIntoConstraints = NO;
tableList.dataSource=self;
tableList.delegate=self;
tableList.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
tableList.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
tableList.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[tableList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
tableList.estimatedRowHeight = 44.0;
tableList.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:tableList];
NSDictionary * views = NSDictionaryOfVariableBindings(tableList);
NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tableList]-0-|" options:0 metrics:nil views:views];
NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[tableList]-0-|"options:0 metrics:nil views:views];
[self.view addConstraints:horizentalConstraint];
[self.view addConstraints:verticalConstraint];
[self initialization];
}
-(void)initialization
{
arrayForBool=[[NSMutableArray alloc]init];
sectionTitleArray=[[NSArray alloc]initWithObjects:
@"Apple",
@"Strawberry",
@"Grapes",
@"Orange",
@"Banana",
nil];
NSArray *new0=[[NSArray alloc]initWithObjects:@"Apple", @"Strawberry",@"Grapes",@"Orange", nil];
NSArray *new1=[[NSArray alloc]initWithObjects:@"Apple1", @"Strawberry1",@"Grapes1",@"Orange1"@"Orange1", nil];
NSArray *new2=[[NSArray alloc]initWithObjects:@"Apple2", @"Strawberry2",@"Grapes2",@"Orange2", nil];
NSArray *new3=[[NSArray alloc]initWithObjects:@"Apple3", @"Strawberry3",@"Grapes3",@"Orange3",@"Orange3", nil];
NSArray *new4=[[NSArray alloc]initWithObjects:@"Apple4", @"Strawberry4",@"Grapes4",@"Grapes4", nil];
totalListOfArray=[[NSMutableArray alloc]init];
[totalListOfArray addObject:new0];
[totalListOfArray addObject:new1];
[totalListOfArray addObject:new2];
[totalListOfArray addObject:new3];
[totalListOfArray addObject:new4];
for (int i=0; i<[sectionTitleArray count]; i++) {
[arrayForBool addObject:[NSNumber numberWithBool:NO]];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[arrayForBool objectAtIndex:section] boolValue]) {
return [respArray count];
}
else
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
// /********** If the section supposed to be closed *******************/
if(!manyCells)
{
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=@"";
}
/********** If the section supposed to be Opened *******************/
else
{
cell.textLabel.text=[NSString stringWithFormat:@"%@",[respArray objectAtIndex:indexPath.row]];
cell.textLabel.font=[UIFont systemFontOfSize:15.0f];
cell.backgroundColor=[UIColor whiteColor];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
cell.textLabel.textColor=[UIColor blackColor];
/********** Add a custom Separator with cell *******************/
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, tableList.frame.size.width, 1)];
separatorLineView.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:separatorLineView];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [sectionTitleArray count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*************** Close the section, once the data is selected ***********************************/
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[tableList reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
return 40;
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 15;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *sectionView1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,40)];
sectionView1.backgroundColor = [UIColor whiteColor];
return sectionView1;
}
#pragma mark - Creating View for TableView Section
//# #e0e0eb place this color
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerview = [[UIView alloc] init];
headerview.backgroundColor = [UIColor lightGrayColor];
headerview.frame = CGRectMake(0, 0, tableView.frame.size.width, 80);
headerview.tag=section;
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, tableList.frame.size.width, 25)];
label1.text = @"Hi";
[headerview addSubview:label1];
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[headerview addGestureRecognizer:headerTapped];
return headerview;
}
#pragma mark - Table header gesture tapped
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
NSLog(@" index ---->>> %ld",(long)indexPath.section);
for (int i=0; i<[sectionTitleArray count]; i++){
if (indexPath.section == i) {
BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
for (int i=0; i<[sectionTitleArray count]; i++) {
if (indexPath.section==i) {
[arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
}else{
[arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:collapsed]];
}
}
[self selectedOption:(int)indexPath.section];
[tableList reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
}
-(NSMutableArray *)selectedOption:(int)mySelectedItemData{
NSLog(@"%d",mySelectedItemData);
NSMutableArray *arrayValue=[[NSMutableArray alloc]init];
respArray=[[NSMutableArray alloc]init];
NSLog(@"%@",[totalListOfArray objectAtIndex:mySelectedItemData]);
NSLog(@"%d",[[totalListOfArray objectAtIndex:mySelectedItemData] count]);
for (int i=0; i< [[totalListOfArray objectAtIndex:mySelectedItemData]count]; i++) {
[arrayValue addObject:[[totalListOfArray objectAtIndex:mySelectedItemData] objectAtIndex:i]];
//[arrayValue addObject:[itemsArray objectAtIndex:mySelectedItemData]];
NSLog(@"array value is =======> %@",arrayValue);
[arrayForBool addObject:[NSNumber numberWithBool:NO]];
}
respArray=[arrayValue mutableCopy];
NSLog(@"respArray items are =====> %@",respArray);
return arrayValue;
}