iOS UITableView numberOfRowsInSection在tableView中的heightForRowAtIndexPath给予BAD ACCESS
当我input下面的if语句[tableView numberOfRowsInSection:0]
时,我收到一个EXC Bad Access错误。我正在计算tableView numberOfRowsInSection
中的UITableView的tableView numberOfRowsInSection
。
我有以下代码来设置第一节的最后一行的高度
-(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; } }
我得到如下图像的错误
我没有发现这种方法适当
iOS UITableView numberOfRowsInSection不良访问
- (int) myNumberOfRowsMethod{ // Here you would usually have some underlying model return [self.myContactsOrWhateverArray count]; }
试试这个方法[self tableView:tableView numberOfRowsInSection:0]
而不是[tableView numberOfRowsInSection:0]
。 它工作正常。
更新您的代码以下
-(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; } }
请尝试这个。 首先创build一个Global NSintegerSectionCount; 然后尝试这个。
- (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]; }
接着,,,,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section==0) { if (indexPath.row==firstSectionCount) { return 44.0f; }else{ return 100; } } else{ return 100; }
希望对你有帮助…
试试这个代码:
-(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; } }
因为这个numberOfRowsInSection
不起作用heightForRowAtIndexPath
委托方法。
尝试这个..
-(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; } }