根据PFRelation检查PFTableViewCell

时间:2013-02-21 06:18:30

标签: iphone ios objective-c parse-platform

我在类别和项目之间有PRelation我希望将PFQueryTableViewController用于向项目添加项目。 PFQueryTableViewController列出所有项目,用户选择要放入类别中的项目。我想弄清楚的是如何确定项目是否在类别中并将PFTableViewCell accessoryType设置为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];
    }

}

应该很简单

干杯

2 个答案:

答案 0 :(得分: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存储在选择数组中,则从remove中删除item,然后添加它并重新加载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];

   }

答案 1 :(得分: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];