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;
}
}
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:
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.
Comments