滚动Objective-C时,附件复选标记消失
我有一个tableview,可以添加和删除多个复选标记。唯一的问题是,如果我放了3个复选标记,然后滚动离开,当我返回时,复选标记就消失了。我在互联网上的任何地方都找不到一个有效的解决方案,我尝试了几种不同的解决方案,但仍然一无所获 这是我在CellForRowatinex中的代码,应该保留复选标记滚动Objective-C时,附件复选标记消失,objective-c,tableview,Objective C,Tableview,我有一个tableview,可以添加和删除多个复选标记。唯一的问题是,如果我放了3个复选标记,然后滚动离开,当我返回时,复选标记就消失了。我在互联网上的任何地方都找不到一个有效的解决方案,我尝试了几种不同的解决方案,但仍然一无所获 这是我在CellForRowatinex中的代码,应该保留复选标记 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = @"contactCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
NSDictionary *contact = [self.tableData objectAtIndex:indexPath.row];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
NSString *firstName = contact[@"firstName"];
nameLabel.text = [firstName stringByAppendingString:[NSString stringWithFormat:@" %@", contact[@"lastName"]]];
UILabel *phoneNumber = (UILabel *)[cell viewWithTag:2];
NSArray *phones = contact[@"phones"];
if ([phones count] > 0) {
NSDictionary *phoneItem = phones[0];
phoneNumber.text = phoneItem[@"value"];
}
UIImageView *cellIconView = (UIImageView *)[cell.contentView viewWithTag:888];
UIImage *image = contact[@"image"];
cellIconView.image = (image != nil) ? image : [UIImage imageNamed:@"smiley-face"];
cellIconView.contentScaleFactor = UIViewContentModeScaleAspectFill;
cellIconView.layer.cornerRadius = CGRectGetHeight(cellIconView.frame) / 2;
// Need to fix
if([checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
下面是didSelectRowAtIndexPath方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell* checkCell = [tableView cellForRowAtIndexPath:indexPath];
if(checkCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
checkCell.accessoryType = UITableViewCellAccessoryNone;
NSMutableArray *i = [[NSMutableArray alloc] init];
for (NSIndexPath *indexPath in [self.tableView indexPathsForSelectedRows]) {
[i addObject:self.tableData[indexPath.row]];
// Go inside pull the numbers from the users and save in an NSArray
// NSArray *contacts = i;
// self.recipients = [[NSMutableArray alloc] init];
for (NSDictionary* dict in i) {
// Grab phones
NSDictionary *contactNumber = [dict objectForKey:@"phones"];
for (NSDictionary* dict2 in contactNumber) {
// Grabs the phone numbers
NSString* value = [dict2 objectForKey:@"value"];
int index = [self.recipients indexOfObject:value];
[self.recipients removeObjectAtIndex:index];
[self.selectedUsers removeObjectAtIndex:index];
NSLog(@"The number that has a checkmark%@", value);
NSLog(@"the array of all%@", self.recipients);
NSLog(@"At index %lu", (unsigned long)[self.recipients indexOfObject:value]);
// [_recipients addObject:value];
}
}
// NSLog(@"Phone Numbers: %@",_recipients);
}
}
else
{
[self getNumber];
NSLog(@"clicking %@", self.recipients);
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
checkedIndexPath = indexPath;
}
}
我找到了解决办法:
您必须将每个indexPath保存到一个数组中。在DidSelectRowatineXpath中输入此代码,然后在CellForRowatineXpath中添加以下代码
if([self.checkedCells containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
也在DidSelectRowatineXpath中
取消选择行时,请确保删除indexPath
if(checkCell.accessoryType == UITableViewCellAccessoryCheckmark) {
checkCell.accessoryType = UITableViewCellAccessoryNone;
[self.checkedCells removeObject:indexPath];
我希望这对某人有帮助。我整天都在努力解决这个问题。也许你应该检查isEqual功能是否达到了你的预期。您可以通过尝试来确保:
如果仍然没有得到预期的结果,可能需要记录section和row的值,看看哪里出错了
请注意,如果出于某种原因_checkedIndexPath是一个弱变量或被释放,此检查将失败
您还可以检查您的单元格在被修改之前是否已正确退出队列,以及是否返回了正确的单元格
当然,如果要存储多个选中行,则需要多个indexPath变量,但一个_checkedIndexPath无法实现 将checkedIndexPath设为@property非原子、强,并在引用它时使用self.checkedIndexPath。在didSelectRowAtIndexPath退出后,您将丢失引用。在CellForRowatineXpath中设置一个断点并查看checkedIndexPath,我打赌它是零。checkedIndexPath来自何处?是否将其存储在类级别?\u checkdIndexPath正在跟踪最后选择的nsindepath。是的,它是我VC的实例变量。你能展示剩下的代码吗?另外,您是否希望多个复选标记起作用?因为我只看到您记录了一个选中的索引路径。请使用CellForRowatineXpath和DidSelectRowatineXpath方法的完整代码更新您的问题。顺便说一句,您确实意识到,使用单个_checkedIndexPath只允许跟踪单个选择。我添加了其余的代码。在NSIndexPath上使用isEqual:很好。考虑到所有其他有用的提示,我觉得否决票相当苛刻。我还碰巧发现了我要检查的事情列表中的实际错误。你是对的,我得到了零分。为什么会发生这种情况?我告诉过你为什么-你没有存储对checkedIndexPath的引用,并且它超出了范围。感谢所有的帮助。看看我想出的解决方案。容器对象
if (_checkedIndexPath.section == indexPath.section &&
_checkedIndexPath.row == indexPath.row)