在我的tableView委托方法中:
我在numberOfRows
numberOfSections
方法中获得了tableView
和delegate
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
和
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath ,
但是在- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
方法中,我尝试了这种方法:
NSString *key = [NSString stringWithFormat:@"%ld", section];
在此行中,它显示错误:线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x)
我拿走了(lldb)
:
(lldb) po [NSString stringWithFormat:@"%ld", section]
error: Trying to put the stack in unreadable memory at: 0x7fff50739eb0.
如果我的numberOfRows
numberOfSections
方法无法获得tableView
和delegate
:
错误不会出现。
以下是我的代码:
- (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;
}
}
答案 0 :(得分:3)
这不是由此代码引起的错误:
NSString *key = [NSString stringWithFormat:@"%ld", section];
致电时:
NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger numberOfSections = [tableView numberOfSections];
在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
中,这将是一个无限循环。
例如(调用其中任何一个都会导致无限循环):
- (void)func1 {
// Code
[self func2];
}
- (void)func2 {
// Code
[self func1];
}
最简单的解决方案是实现您的代码如下:
- (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;
}
}