1

PRelation在类别和项目之间有一个我正在寻找PFQueryTableViewController用于将项目添加到类别的项目。PFQueryTableViewController列出所有项目,用户选择要放入类别的项目。我想弄清楚的是如何确定项目是否在类别中并将PFTableViewCell附件类型设置为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

我遇到过类似的情况。我制作了一个容器来保存选定的 indexPaths。

@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

下面的这种技术会根据部分和行号跟踪先前选择和未选择的项目。
由于 tableview 是基于已经存在并且我们拥有的列表建立的......
在要执行的操作时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 回答