1

9 つのセルを持つ単純な UITableView があります。スクロールでテーブルを上下に移動すると、EXE の不正なアクセスが発生します。NSZombieMode は cellForRowAtIndexMethod を指します。

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

cell.textLabel.text = [lineArray objectAtIndex:indexPath.row];
cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

誰が何が間違っているのか提案できますか?

4

3 に答える 3

1

私の推測では、範囲外の要素にアクセスしようとしていますlineArray

IE:indexPath.rowに要素が 3 つしかない場合に 6 を返しますlineArray

cellForRowAtIndexPathより多くの行 (たとえば、indexPath.row > 3 の行) で呼び出されるようにトリガーするときに、下にスクロールすると発生します。

もう 1 歩進んで、おそらく静的に を返していると推測しますnumberOfRowsForSection

に設定するとlineArray.count修正されるはずです。

于 2012-09-01T19:46:41.837 に答える
1

ARCが無効になっているautorelease場合は、作成時に追加してくださいcell

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier] autorelease];

これが漏れの原因である可能性があります。lineArrayivar のように使用され、おそらくこの配列はある時点でリリースされたので、確認してください。

于 2012-09-01T19:41:27.707 に答える
0

私の理解によると:-

1) lineArray には 9 個のアイテムがありますが、numberOfRowsInSection では、配列内のアイテムよりも多くの rowCount を返しているため、クラッシュして ceelForRowAtIndex を指します。

2)理解のためのサンプルコードは次のとおりです:-

- (void)viewDidLoad
{
    [super viewDidLoad];
    lineArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    tableView1 = [[UITableView alloc]init];
    tableView1.delegate = self;
    tableView1.dataSource = self;
    tableView1 .frame =self.view.frame;
    [self.view addSubview:tableView1];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return  [lineArray count];

    //return ;
}


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

    cell.textLabel.text = [lineArray objectAtIndex:indexPath.row];
    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
于 2012-11-27T05:10:14.857 に答える