1

PRelationカテゴリと、カテゴリにアイテムをPFQueryTableViewController追加するために使用するアイテムを作成しようとしています。すべてのPFQueryTableViewControllerアイテムが一覧表示され、ユーザーはカテゴリに入れるアイテムを選択します。私が理解しようとしているのは、アイテムがカテゴリに含まれているかどうかを判断し、PFTableViewCellaccessoryTypeをに設定する方法だけです。UITableViewCellAccessoryCheckmark

現在のコード:

- (PFTableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    static NSString * CellIdentifier = @"Cell";

    BOOL itemInCategory = ?; //This is where I am determining the hierarchy

    PFTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.textLabel.text = [sendingObject valueForKey:@"Title"];
    cell.accessoryType = itemInCategory ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;

}

編集

カテゴリからアイテムを削除するためのサポートも必要です。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    BOOL itemInCategory = ?;

    if (itemInCategory) {

        //Remove Item?

    } else {
        PFRelation * relation = [sendingObject relationforKey:@"Items"];
        [relation addObject:[self.objects objectAtIndex:[indexPath row]]];
        [self save];
    }

}

かなりシンプルなはずです

乾杯

4

2 に答える 2

1

私は同様の状況に遭遇しました。選択した indexPath を保持するためのコンテナーを作成しました。

@property (nonatomic, strong) NSMutableArray*selectedIndexPaths;

選択コンテ​​ナを初期化する

- (void)viewDidLoad
{
    self.selectedIndexPaths = [@[] mutableCopy];
}

特定の indexPath が選択コンテ​​ナー配列に格納されているかどうかを確認する

- (PFTableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    static NSString * CellIdentifier = @"Cell";

    BOOL itemInCategory = [self.selectedIndexPaths containsObject:indexPath];

    PFTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.textLabel.text = [sendingObject valueForKey:@"Title"];
    cell.accessoryType = itemInCategory ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;

}

indexPath が選択配列に格納されている場合は、リレーションからアイテムを削除します。そうでない場合は、追加して tableView をリロードします。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

       BOOL itemInCategory = [self.selectedIndexPaths containsObject:indexPath];
       PFRelation * relation = [sendingObject relationforKey:@"Items"];

       if (itemInCategory) {

           //Remove the item from selectedIndex container and also relation
           [self.selectedIndexPaths removeObject:indexPath];
           [relation removeObject:self.objects[indexPath.row]];

       } 
       else {

           [self.selectedIndexPaths addObject:indexPath];
           [relation addObject:self.objects[indexPath.row]];
           [self save];
       }

       [tableView reloadData];

   }
于 2013-03-05T15:04:32.793 に答える
0

以下のこの手法は、セクションと行番号に基づいて、以前に選択されたアイテムと選択されていないアイテムを追跡します。
テーブルビューは、すでに保持されているリストに基づいて確立されているため
、実行されるアクションの時点で、checkMarkSectionsDictionaryは選択されたアイテムのリストを提供します。
[checkMarkSectionsDictionary ALLKEYS]// セクションのキーを返し、
各セクションに対して行を選択します。

 NSMutableDictionary *checkMarkSectionsDictionary = nil; //need to be defined at class level


if ([checkMarkSectionsDictionary objectForKey:[NSNumber numberWithInt:indexPath.section]]) {
    NSDictionary * checkMarkRowDictionary = [checkMarkSectionsDictionary objectForKey:[NSNumber numberWithInt:indexPath.section]];
    if ([checkMarkRowDictionary objectForKey:[NSNumber numberWithInt:indexPath.row]]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];            
    }
}
else{
        // category was not found in selected items
}



//DID SELECT


//CHECK / UNCHECK
if ([checkMarkSectionsDictionary objectForKey:[NSNumber numberWithInt:indexPath.section]]) {
    //SECTION EXISTS
    NSMutableDictionary * checkMarkRowDictionary = [checkMarkSectionsDictionary objectForKey:[NSNumber numberWithInt:indexPath.section]];
    if ([checkMarkRowDictionary objectForKey:[NSNumber numberWithInt:indexPath.row]]) {
        [checkMarkRowDictionary removeObjectForKey:[NSNumber numberWithInt:indexPath.row]];
    }
    else{
        [checkMarkRowDictionary setObject:[NSNumber numberWithInt:indexPath.row] forKey:[NSNumber numberWithInt:indexPath.row]];
    }
    if (checkMarkSectionsDictionary.count == 0) {
        [checkMarkSectionsDictionary removeObjectForKey:[NSNumber numberWithInt:indexPath.section]];
    }
}

else{

    //SECTION DOESNOT EXIST CREATE IT AND SET THE SELECTED ROW
    NSMutableDictionary *checkMarkRowDictionary = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:indexPath.row] forKey:[NSNumber numberWithInt:indexPath.row]];
    [checkMarkSectionsDictionary setObject:checkMarkRowDictionary forKey:[NSNumber numberWithInt:indexPath.section]];

}

[_tableView reloadData];
于 2013-02-26T22:16:27.683 に答える