首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于PFTableViewCell的PFRelation检查

基于PFTableViewCell的PFRelation检查
EN

Stack Overflow用户
提问于 2013-02-21 06:18:29
回答 2查看 236关注 0票数 1

我有一个PRelation之间的类别和项目,我想要做一个PFQueryTableViewController,以用于添加项目到类别。PFQueryTableViewController列出所有项目,用户选择要放入类别中的项目。我想弄清楚的是如何确定该项目是否在类别中,并将PFTableViewCell accessoryType设置为UITableViewCellAccessoryCheckmark

现行法典:

代码语言:javascript
复制
- (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;

}

编辑

我还需要一些帮助,将该项目从类别中删除:

代码语言:javascript
复制
- (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];
    }

}

应该很简单

干杯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-05 15:04:32

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

代码语言:javascript
复制
@property (nonatomic, strong) NSMutableArray*selectedIndexPaths;

初始化选择容器

代码语言:javascript
复制
- (void)viewDidLoad
{
    self.selectedIndexPaths = [@[] mutableCopy];
}

检查特定的indexPath是否存储在选择容器数组中。

代码语言:javascript
复制
- (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。

代码语言:javascript
复制
- (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
EN

Stack Overflow用户

发布于 2013-02-26 22:16:27

下面的技术根据节号和行号跟踪以前选择的和未选中的项。

因为一个表视图是基于一个已经持续存在的列表建立的,所以我们有.

在执行操作时,"**checkMarkSectionsDictionary**"将向我们提供所选项目的列表。

代码语言:javascript
复制
 [checkMarkSectionsDictionary ALLKEYS]// returns us the key of the sections

对于每一节,我们将有选定的行。

代码语言:javascript
复制
 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];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14995790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档