2016-07-28 162 views
2

在我的tableView委托方法:问:通过tableView的indexPath获取行数`尝试将堆放置在不可读的内存中:`error。

我得到numberOfRowsnumberOfSections在我tableViewdelegate方法:

- (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. 

如果我没有得到numberOfRowsnumberOfSections在我的tableViewdelegate方法:      错误不会出现。下面

是我的代码:

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

回答

3

这是不是由于这个代码错误:

NSString *key = [NSString stringWithFormat:@"%ld", section]; 

你的代码是由系统中的无限循环和结束运行。请看下面: enter image description here

当你拨打:

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

很好的回答,谢谢@ zylenv – aircraft

相关问题