无法计算出代码中整数的值变化

Unable to figure out the value change of the integer in the code

我有这段代码,我正在努力理解它。这有一个 int 值 'currentExpandIndex' ,我无法弄清楚为什么它会改变,因为我没有找到它的正确启动。 int 首先被赋予值 -1,但稍后在代码中 int 值根据索引路径发生变化。我没有找到 code.Kindly 中声明的 int 和索引路径之间的关系告诉我,为什么

密码是:

  @interface AccordionTableViewController : UITableViewController {
NSArray *topItems;
NSMutableArray *subItems; // array of arrays

int currentExpandedIndex;
}

@end

//.m文件

- (id)init {
self = [super init];

if (self) {
    topItems = [[NSArray alloc] initWithArray:[self topLevelItems]];
    subItems = [NSMutableArray new];
    currentExpandedIndex = -1;
     NSLog(@"currenyt index -init is %d",currentExpandedIndex);
    for (int i = 0; i < [topItems count]; i++) {
        [subItems addObject:[self subItems]];
    }
}
return self;
 }

   #pragma mark - Data generators

  - (NSArray *)topLevelItems {
NSMutableArray *items = [NSMutableArray array];

for (int i = 0; i < NUM_TOP_ITEMS; i++) {
    [items addObject:[NSString stringWithFormat:@"Item %d", i + 1]];
}

return items;
}

 - (NSArray *)subItems {
NSMutableArray *items = [NSMutableArray array];
int numItems = arc4random() % NUM_SUBITEMS + 2;

for (int i = 0; i < numItems; i++) {
    [items addObject:[NSString stringWithFormat:@"SubItem %d", i + 1]];
}

return items;
}

  #pragma mark - View management

- (void)viewDidLoad {
[super viewDidLoad];

NSLog(@"currenyt index -view did load is %d",currentExpandedIndex);

}

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

 #pragma mark - Table view data source

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 NSLog(@"currenyt index -no of rows in section is %d",currentExpandedIndex);

return [topItems count] + ((currentExpandedIndex > -1) ? [[subItems objectAtIndex:currentExpandedIndex] count] : 0);
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ParentCellIdentifier = @"ParentCell";
static NSString *ChildCellIdentifier = @"ChildCell";
  NSLog(@"currenyt index-cell for row at index is %d",currentExpandedIndex);

BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

UITableViewCell *cell;

if (isChild) {
    cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
}
else {
    cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
}


if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier] autorelease];
}

if (isChild) {
    cell.detailTextLabel.text = [[subItems objectAtIndex:currentExpandedIndex] objectAtIndex:indexPath.row - currentExpandedIndex - 1];
}
else {
    int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex)
    ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]
    : indexPath.row;

    cell.textLabel.text = [topItems objectAtIndex:topIndex];
    cell.detailTextLabel.text = @"";
}

return cell;
}

  #pragma mark - Table view delegate

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

if (isChild) {
    NSLog(@"A child was tapped, do what you will with it");
     NSLog(@"currenyt index -did select is %d",currentExpandedIndex);
    return;
}
 NSLog(@"currenyt index -did select out is %d",currentExpandedIndex);
[self.tableView beginUpdates];

if (currentExpandedIndex == indexPath.row) {
    [self collapseSubItemsAtIndex:currentExpandedIndex];
    currentExpandedIndex = -1;
}
else {

    BOOL shouldCollapse = currentExpandedIndex > -1;

    if (shouldCollapse) {
        [self collapseSubItemsAtIndex:currentExpandedIndex];
    }

    currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

    [self expandItemAtIndex:currentExpandedIndex];
}

[self.tableView endUpdates];

}

- (void)expandItemAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
NSArray *currentSubItems = [subItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentSubItems count]; i++) {
    [indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
//[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[indexPaths release];
}

 - (void)collapseSubItemsAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
    [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[indexPaths release];
}

didSelectRowAtIndexPath: 中你有这一行:

    currentExpandedIndex = 
        (shouldCollapse && indexPath.row > currentExpandedIndex) ? 
    indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : 
    indexPath.row;

此行将 indexPath.rowindexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] 分配给 currentExpandedIndex

currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

在每个 didSelectRow 上它都会改变。

首先,在-init设置断点。根据您创建此控制器的方式,它可能不会被调用,在这种情况下它将是 0。如果是这种情况,请尝试 -initWithCoder:。否则,我只在一个地方看到这个集合:-tableView:didSelectRowAtIndexPath:,它可以设置到行或indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]。根据您对 table 所做的其他操作,这可能是简化部分或可扩展的单个行的好方法。