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

aircraft

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;
    }
}
zylenv

This is not a error caused 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 cause 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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get indexpath outside of any tableView function

From Dev

Get indexpath outside of any tableView function

From Dev

Error Trying to get cell data value JavaFX TableView

From Dev

Trying to use getIntent() within a custom dialog comes up as an error. Solution to get around this error?

From Dev

Error when trying to save indexPath to NSUserDefaults

From Dev

Memory error in stack

From Dev

How to get the indexpath of UISwitch in dynamic tableview using swift?

From Dev

How to get IndexPath first section tableview of subcell using Objective C?

From Dev

Count Rows Error in Trigger

From Dev

mysqli count rows error

From Dev

instantly scroll tableView to indexPath

From Dev

instantly scroll tableView to indexPath

From Dev

indexPath comparison in TableView

From Dev

How to get the count of rows

From Dev

Get rows with maximum count

From Dev

tableView:indexPath* always returning a nil IndexPath

From Dev

Trying to run a Program called worldforge but it comes up with an error

From Dev

Trying to add values in python but comes up with int not callable error

From Dev

Trying to count and return a number of rows in a table

From Dev

Trying to count and return a number of rows in a table

From Dev

In EXCEL trying to count rows that meet 3 criteria:

From Dev

How the offset comes in stack?

From Dev

How can I get each rows of tableview?

From Dev

How can I get each rows of tableview?

From Dev

Getting the Row for Section indexPath in a tableView

From Dev

IndexPath Swift TableView clicked column

From Dev

Get the name of the rows with COUNT in SQL

From Dev

Get Similar rows count in mysql

From Dev

Get highest count for specific rows

Related Related

HotTag

Archive