简体   繁体   English

iOS UITableView numberOfRowsInSection在tableView heightForRowAtIndexPath中给予不良访问

[英]iOS UITableView numberOfRowsInSection in tableView heightForRowAtIndexPath give BAD ACCESS

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 . 我在[tableView numberOfRowsInSection:0]下输入if语句时收到EXC错误访问错误。我正在计算heightForRowAtIndexPath内部的UITableView的tableView numberOfRowsInSection

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 iOS UITableView numberOfRowsInSection错误访问

- (int) myNumberOfRowsMethod{
    // Here you would usually have some underlying model 
    return [self.myContactsOrWhateverArray count];
}

Try this method [self tableView:tableView numberOfRowsInSection:0] instead of [tableView numberOfRowsInSection:0] . 尝试使用此方法[self tableView:tableView numberOfRowsInSection:0]而不是[tableView numberOfRowsInSection:0] It's works fine. 很好

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

Please Try With This. 请尝试与此。 Make one Global NSinteger firstSectionCount; 首先使一个全局NSintegerSectionCount; 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... 希望这个能对您有所帮助...

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. 因为此numberOfRowsInSection不起作用heightForRowAtIndexPath委托方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS UITableView numberOfRowsInSection错误访问 - iOS UITableView numberOfRowsInSection BAD ACCESS 使用UITableView时,ios-tableView:numberOfRowsInSection…NSInvalidArgumentException - ios - tableView:numberOfRowsInSection …NSInvalidArgumentException when working with UITableView iOS中的EXC_BAD_ACCESS为heightForRowAtIndexPath - EXC_BAD_ACCESS in heightForRowAtIndexPath iOS 表格视图中的“ EXEC_BAD_ACCESS”,“ [[TasksPageViewController tableView:numberOfRowsInSection:]” - 'EXEC_BAD_ACCESS' in Table View, “[TasksPageViewController tableView:numberOfRowsInSection:]” 什么时候是tableView:numberOfRowsInSection:在UITableView中调用? - When is tableView:numberOfRowsInSection: called in UITableView? 调用numberOfRowsInSection:从heightForRowAtIndexPath - Calling numberOfRowsInSection: from heightForRowAtIndexPath 如何获取tableView中的单元格对象:(UITableView *)tableView heightForRowAtIndexPath函数? - How to get the cell object in tableView:(UITableView *)tableView heightForRowAtIndexPath function? iOS触发单细胞tableView:heightForRowAtIndexPath: - iOS trigger single cell tableView:heightForRowAtIndexPath: TableView和heightForRowAtIndexPath - TableView and heightForRowAtIndexPath iOS Swift:TableView numberOfRowsInSection返回错误的计数 - iOS Swift: TableView numberOfRowsInSection returning wrong count
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM