QQ726535609 QQ726535609 - 4 months ago 130
iOS Question

Q:Get rows count by indexPath of tableView comes `Trying to put the stack in unreadable memory at:` error.

In my tableView delegate methods:

I get

numberOfRows
and
numberOfSections
in my
tableView
delegate
methods:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


and

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
,

but in the
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
method, I tried this method:

NSString *key = [NSString stringWithFormat:@"%ld", section];


In this line , it shows the error:Thread 1:EXC_BAD_ACCESS(code=2,address=0x)
and I taken the
(lldb)
:

(lldb) po [NSString stringWithFormat:@"%ld", section]
error: Trying to put the stack in unreadable memory at: 0x7fff50739eb0.


If I don't get
numberOfRows
and
numberOfSections
in my
tableView
delegate
methods:
    the error will not appear.

The below is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];

NSInteger numberOfSections = [tableView numberOfSections];

if (indexPath.row == 0 && numberOfRows != 1) {

cell.textLabel.text = [_arr objectAtIndex:indexPath.row];
}



return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];

NSInteger numberOfSections = [tableView numberOfSections];

if (indexPath.row == 0 && numberOfRows != 1) {

return 40;
}else {
return 5;
}

}

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

//NSString *key = [NSString stringWithFormat:@"%d", (int)section];

NSString *key = [NSString stringWithFormat:@"%ld", section]; // this line shows the error.


BOOL folded = [[_foldInfoDic objectForKey:key] boolValue];

if (section == 0) {
return folded?_arr.count:1;
} else if (section == 1) {
return folded?_arr.count:1;
} else if (section == 2) {
return folded?_arr.count:1;
} else {
return folded?_arr.count:0;
}
}

Answer

This is not a error cased by this code:

NSString *key = [NSString stringWithFormat:@"%ld", section];

Your code is run in an endless loop and end by the system. Look at the below: enter image description here

When you call:

 NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];
 NSInteger numberOfSections = [tableView numberOfSections];

in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath, this will case an endless loop.

For Example(call either of them will case an endless loop):

- (void)func1 {
    // Code
    [self func2];
}
- (void)func2 {
    // Code
    [self func1];
}

The Simplest solution is implement you code as below:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];

    NSInteger numberOfSections = [self numberOfSectionsInTableView:tableView];

    if (indexPath.row == 0 && numberOfRows != 1) {

        cell.textLabel.text = [_arr objectAtIndex:indexPath.row];
    }



    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger numberOfRows = [self tableView:self numberOfRowsInSection:[indexPath section]];

    NSInteger numberOfSections = [self numberOfSectionsInTableView:tableView];

    if (indexPath.row == 0 && numberOfRows != 1) {

        return 40;
    }else {
        return 5;
    }

}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //NSString *key = [NSString stringWithFormat:@"%d", (int)section];

    NSString *key = [NSString stringWithFormat:@"%ld", section];  // this line shows the error.


    BOOL folded = [[_foldInfoDic objectForKey:key] boolValue];

    if (section == 0) {
        return folded?_arr.count:1;
    } else if (section == 1) {
        return folded?_arr.count:1;
    } else if (section == 2) {
        return folded?_arr.count:1;
    } else {
        return folded?_arr.count:0;
    }
}
Comments