2

配列の内容を uitableview に表示しようとしています。値を個別に割り当てると正常に動作しますが、配列からそれらを表示しようとすると、xcode は (lldb) 以外のデバッグ レポートに何も表示されずに終了します。テーブルビューのあるページに移動するまで、コンパイルしてうまく動作します。エラーを調べたところ、メモリ割り当てに関連しているようですが、一部の JSON クラスが動作するために ARC を有効にする必要があるため、手動で何も解放できません。誰でも見て、問題の可能性があるものを確認できますか。

- (void)viewDidLoad
{
    [super viewDidLoad];
    theArray = [[NSArray alloc] initWithObjects:@"1","2","3","4",nil];

    // Do any additional setup after loading the view.
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [theArray count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    // Set up the cell...
    self.recentSearchesTable.backgroundColor =[UIColor clearColor];
    self.recentSearchesTable.separatorColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    cell.textLabel.text = [theArray objectAtIndex:indexPath.row];

    cell.contentView.backgroundColor = [UIColor clearColor];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // open a alert with an OK and cancel button
    NSString *alertString = [NSString stringWithFormat:@"Clicked on row #%d", [indexPath row]];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
    [alert show];

}
4

3 に答える 3

4

配列にオブジェクト以外のアイテムが含まれている場合はviewDidLoad、それも変更します。

theArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];

@配列内の各項目の前に注意してください。これは、それらがNSStringリテラル(c文字列ではない)であることを示しています。

于 2013-01-23T20:18:57.657 に答える
1

まず第一に、外部クラスが原因でプロジェクトの ARC を無効にする必要はありません。これらのクラスは、-fno-objc-arc フラグを設定して除外できます。

回答に戻る 前のセルから再利用できる場合は、新しいセルを割り当てる必要があります。 dequeueReusableCellWithIdentifier:CellIdentifier がセルを返さない場合は、新しいセルを割り当てる必要があります。

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil)  {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier] autorelease];
  }

それとは別に、viewDidLoad でデータソース配列を初期化するときは、NSString を示すために各要素の前に @ を追加する必要があります。

于 2013-01-23T20:11:40.200 に答える
1

ここで最初に 2 つの間違いを犯しました。この方法で配列を初期化する必要があります。

theArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];

もう1つの間違いは、セルがnilのときにセルを初期化する必要があることです

if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

コード全体が必要な場合は、このように実装する必要があります。

- (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] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    // Set up the cell...

    cell.textLabel.text = [theArray objectAtIndex:indexPath.row];

    cell.contentView.backgroundColor = [UIColor clearColor];

    return cell;
}
于 2013-01-23T20:31:16.400 に答える