我有一个tableview,我可以添加和删除多个复选标记。唯一的问题是如果我放了3个复选标记并滚动,当我返回时,复选标记消失了。我无法在互联网上的任何地方找到有效的解决方案,而且我尝试了几种变化,但仍然没有。
这是我在cellForRowAtIndex中的代码,它应该保留复选标记。
-(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保存到一个数组中(将此代码放在didSelectRowAtIndexPath中),然后在cellForRowAtIndexPath中添加以下代码
if([self.checkedCells containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
也在didSelectRowAtIndexPath中 确保在取消选择行时删除indexPath。
if(checkCell.accessoryType == UITableViewCellAccessoryCheckmark) {
checkCell.accessoryType = UITableViewCellAccessoryNone;
[self.checkedCells removeObject:indexPath];
我希望这有助于某人。我一整天都在努力。
答案 0 :(得分:0)
也许您应该检查 isEqual
功能是否符合您的预期。您可以通过尝试确保:
<击> 撞击>
<击>if (_checkedIndexPath.section == indexPath.section &&
_checkedIndexPath.row == indexPath.row)
击> <击> 撞击>
如果您仍然无法获得预期结果,请记录section
和row
的值,以查看其出错的位置。
请注意,如果由于某种原因_checkedIndexPath
是弱变量或被取消分配,则此检查将失败。
您还可以检查您的细胞在被修改之前是否正确出列,并且您正在返回正确的细胞。
如果你想存储多个已检查的行,当然,你需要多个indexPath变量(只有一个_checkedIndexPath
不会这样做)。
答案 1 :(得分:0)
让checkedIndexPath
成为@property (nonatomic, strong)
并在您引用self.checkedIndexPath
时使用didSelectRowAtIndexPath
。在cellForRowAtIndexPath
退出后,您将失去参考。在checkedIndexPath
设置断点并查看 public class CustomAuthorize : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
return /*Chech if user is loged in*/
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.HttpContext.Response.Redirect(URL TO REDIRECT);
}
}
,我打赌它是零。