问题
I have receive a EXC Bad Access error when I enter the if statement below [tableView numberOfRowsInSection:0]
.I'm calculating the tableView numberOfRowsInSection
of the UITableView inside the heightForRowAtIndexPath .
I have got following code to set height for last row of first section
-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==0) {
NSInteger lastRowIndex = [tableView numberOfRowsInSection:0] - 1;
if (indexPath.row==lastRowIndex) {
return 44.0f;
}else{
return 100;
}
}else{
return 100;
}
}
I got the error as below image
I didnt find this method appropriate
iOS UITableView numberOfRowsInSection BAD ACCESS
- (int) myNumberOfRowsMethod{
// Here you would usually have some underlying model
return [self.myContactsOrWhateverArray count];
}
回答1:
Try this method
[self tableView:tableView numberOfRowsInSection:0]
instead of [tableView numberOfRowsInSection:0]
. It's works fine.
回答2:
Update your code to following
-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==0) {
NSInteger lastRowIndex = [self numberOfRowsInSection:indexPath.section] - 1;
if (indexPath.row==lastRowIndex) {
return 44.0f;
}else{
return 100;
}
}else{
return 100;
}
}
回答3:
Please Try With This. Make one Global NSinteger firstSectionCount; and then try with this.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section==0){
firstSectionCount = [ storeArr count];
return [ storeArr count]+1;
}else{
return [ [[itemArr objectAtIndex:section-1] objectForKey:@"itemList"]count];
}
and then,,,,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {
if (indexPath.row==firstSectionCount) {
return 44.0f;
}else{
return 100;
}
}
else{
return 100;
}
Hope this will help you...
回答4:
Try this code:
-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==0) {
long lastRowIndex = [self.yourArray count]-1;
// other wise use this line
// long lastRowIndex = [self.yourArray lastObject]-1;
if (indexPath.row==lastRowIndex) {
return 44.0f;
}else{
return 100;
}
}else{
return 100;
}
}
Because this numberOfRowsInSection
not work heightForRowAtIndexPath
delegate method.
回答5:
Try this..
-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==0) {
NSInteger lastRowIndex = yourArray.count - 1;
if (indexPath.row==lastRowIndex) {
return 44.0f;
}else{
return 100;
}
}else{
return 100;
}
}
来源:https://stackoverflow.com/questions/31667986/ios-uitableview-numberofrowsinsection-in-tableview-heightforrowatindexpath-give