2

私はいくつかのセクションといくつかの行を持つ各セクションを持つtableViewを持っています.すべての行にはお気に入りのボタンがあります.ボタンがクリックされると、行はお気に入りセクションに追加されます. (最初は空です)。

私はいくつかのコードを書きましたが、問題は iOS 5 シミュレーターで動作し、無効な tableView 更新エラーで iOS 6 シミュレーターでクラッシュすることです。

誰かが問題の場所を教えてもらえますか?

-(void)moveRowToFavourites:(id)sender{
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    [tempArray addObject:[NSIndexPath indexPathForRow:favouritesArray.count inSection:0]];
    [favouritesArray insertObject:cell.textLabel.text atIndex:favouritesArray.count];
    [[self tableView] beginUpdates];
    [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationFade];
    [[self tableView] endUpdates];
}

編集

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if(searching){
        return [self.searchResults count];
    }
    else{
        if ([[arrayForBool objectAtIndex:section] boolValue]) {
            if(section == 0){
                return favouritesArray.count;
            }else{
                NSString* key = [self.proCategoriesArray objectAtIndex:section - 1];
                NSArray *aArray = [sectionContentDict valueForKey:key];
                return aArray.count;
            }
        }
        return 1;
    }
}
4

2 に答える 2

0

データ ソースがセクションと行の数として 0 を返しているようです。行を正しく挿入/削除していません。新しい行を挿入すると、データ ソース メソッドが再度呼び出されます。たとえば、行数が 4 になるようにオブジェクトを挿入しようとすると、データ ソースtableView:numberOfRowsInSection:は 3 を返します。データソースが約束するオブジェクトをさらに追加しようとしているため、例外が発生します。

テーブル ビューを更新しようとすると、これらすべてのデータ ソース メソッドが再度呼び出されます。

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView;
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath;

それらが正しい値を返すことを確認する必要があります。あなたもそれらを実装していないと思います。

編集

問題は、新しいオブジェクトを に挿入しているだけですがfavouritesArray、これは によって返される行数には影響しませんtableView:numberOfRowsInSection:。を返していません。例外は、テーブル ビューに必要な行数よりも少ない数を返すという favouritesArray.count事実によるものです。tableView:numberOfRowsInSection:

あなたの場合、 を呼び出す必要さえないと思いinsertRowsAtIndexPaths:ます。データソースですべてを実行できます。

-(void)moveRowToFavourites:(id)sender {
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
    [favouritesArray insertObject:cell.textLabel.text atIndex:favouritesArray.count];
    [[self tableView]reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return favouritesArray.count;
}

では、オブジェクトが取得されたフォームをテキストとしてtableView:cellForRowAtIndexPath:含むセルを返す必要があります。favouritesArray

于 2013-07-26T11:08:44.770 に答える
0

次のように呼び出す必要があります。

[favouritesArray addObject:cell.textLabel.text];
[tableView reloadData];

この関数を実装したことを確認してください:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    switch(section){
         case 0:
            return favouritesArray.count;
         //Your other sections have to placed here, too
         default:
            return 0;
    }
}

その後、あなたのtableViewはそれ自体を更新する必要があります。問題は、セル全体を挿入することですが、配列にデータを挿入するだけです。それが役に立てば幸い!

于 2013-07-26T11:26:19.567 に答える