我有一个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];
}
}应该很简单
干杯
发布于 2013-03-05 15:04:32
我也遇到过类似的情况。我制作了一个容器来保存选定的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-02-26 22:16:27
下面的技术根据节号和行号跟踪以前选择的和未选中的项。
因为一个表视图是基于一个已经持续存在的列表建立的,所以我们有.
在执行操作时,"**checkMarkSectionsDictionary**"将向我们提供所选项目的列表。
[checkMarkSectionsDictionary ALLKEYS]// returns us the key of the sections对于每一节,我们将有选定的行。
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];https://stackoverflow.com/questions/14995790
复制相似问题