向上或向下滚动时,UITableView会隐藏附件复选标记

时间:2016-07-26 08:45:17

标签: ios objective-c uitableview

您好我在堆栈溢出中尝试了多个答案,但是当我滚动表视图时,当我向上或向下滚动时,附件复选标记消失。

This One

我的代码---

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PackageTableCell *cell = (PackageTableCell *)[tableView dequeueReusableCellWithIdentifier:packageCell];

if (cell == nil)
{
    cell = [[PackageTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:packageCell];
}
cell.backgroundColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

JobAndPackDetails *objJobAndPackDetails =[self.arrayOfJobList objectAtIndex:indexPath.row];

if ([objJobAndPackDetails.jobType isEqualToString:@"JPK"])
{
    [cell.imgJPK setImage:[UIImage imageNamed:@"vs_package.png"]];
}
else{
     [cell.imgJPK setImage:nil];
}
 NSIndexPath *indexPathP = [self.tableView indexPathForSelectedRow];
[self.tableView selectRowAtIndexPath:indexPathP animated:NO scrollPosition:UITableViewScrollPositionNone];

NSNumber *rowNsNum = [NSNumber numberWithInteger:indexPath.row];

if ( [self.arrayOfJobList containsObject:rowNsNum]  )
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark ;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}


[cell.lblJobName setText:objJobAndPackDetails.jobName];
[cell.lblJobDescription setText:objJobAndPackDetails.jobDesc];
[cell.lblJobCreated setText:objJobAndPackDetails.jobType];
[cell.lblJobSK setText:[NSString stringWithFormat:@"%.0f",objJobAndPackDetails.jobSK]];
[cell.imgVehicle setImage:[UIImage imageNamed:@"placeholder.png"]];

return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path
{
PackageTableCell *cell = [tableView cellForRowAtIndexPath:path];
NSString *str_JobType = cell.lblJobCreated.text;
NSString *str_JobSK = cell.lblJobSK.text;


if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
    cell.accessoryType = UITableViewCellAccessoryNone;
    if ([str_JobType isEqualToString:@"JOB"])
    {
        [self.array_Job removeLastObject];
        [self.array_JobSK removeLastObject];
        NSLog(@"JOB :%@",self.array_Job);
        NSLog(@"JOB_SK :%@",self.array_JobSK);

    }
    else
    {
        [self.array_Package removeLastObject];
        [self.array_PackageSK removeLastObject];

        NSLog(@"PACKAGE :%@",self.array_Package);
        NSLog(@"Package_SK :%@",self.array_PackageSK);
    }
}
else
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    NSLog(@"Selected Job Type Text :%@",str_JobType);


    if ([str_JobType isEqualToString:@"JOB"])
    {
        [self.array_Job addObject:str_JobType];
        [self.array_JobSK addObject:str_JobSK];

        NSLog(@"JOB :%lu",(unsigned long)self.array_Job.count);
        NSLog(@"JOB_SK :%@",self.array_JobSK);



        NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];

        for (int i = 1; i <= self.array_Job.count; i ++)
        {
            [tmpDict setObject:[NSString stringWithFormat:@"%d",0] forKey:@"AppointmentJobSK"];
            [tmpDict setObject:[NSString stringWithFormat:@"%d",0] forKey:@"AppointmentSK"];
            [tmpDict setObject:str_JobType forKey:@"JobType"];
            [tmpDict setObject:str_JobSK forKey:@"JobTypeSK"];
            [tmpDict setObject:[NSString stringWithFormat:@"%d",5] forKey:@"UserCreated"];
            [tmpDict setObject:[NSString stringWithFormat:@"2016-04-14 10:00:00"] forKey:@"DateCreated"];

            NSMutableArray *dictAllKeys=[NSMutableArray arrayWithArray:[tmpDict allKeys]];
            NSMutableArray *dictAllValues=[NSMutableArray arrayWithArray:[tmpDict allValues]];
            Job_keysAndValues=[NSMutableArray arrayWithArray:[dictAllKeys arrayByAddingObjectsFromArray:dictAllValues]];
            NSLog(@"Key and Values :%@",Job_keysAndValues);
        }
        Job_dict = tmpDict;
        NSLog(@"Dict :%@",Job_dict);
    }
    else
    {
        [self.array_Package addObject:str_JobType];
        [self.array_PackageSK addObject:str_JobSK];

        NSLog(@"PACKAGE :%@",self.array_Package);
        NSLog(@"Package_SK :%@",self.array_PackageSK);

        NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
        for (int i = 1; i <= self.array_Package.count; i ++)
        {
            [tmpDict setObject:[NSString stringWithFormat:@"%d",0] forKey:@"AppointmentJobSK"];
            [tmpDict setObject:[NSString stringWithFormat:@"%d",0] forKey:@"AppointmentSK"];
            [tmpDict setObject:str_JobType forKey:@"JobType"];
            [tmpDict setObject:str_JobSK forKey:@"JobTypeSK"];
            [tmpDict setObject:[NSString stringWithFormat:@"%d",5] forKey:@"UserCreated"];
            [tmpDict setObject:[NSString stringWithFormat:@"2016-04-14 10:00:00"] forKey:@"DateCreated"];

            NSMutableArray *dictAllKeys=[NSMutableArray arrayWithArray:[tmpDict allKeys]];
            NSMutableArray *dictAllValues=[NSMutableArray arrayWithArray:[tmpDict allValues]];
            Package_keysAndValues=[NSMutableArray arrayWithArray:[dictAllKeys arrayByAddingObjectsFromArray:dictAllValues]];
        }
        Package_dict = tmpDict;
        NSLog(@"Dict :%@",Package_dict);

    }

    NSMutableDictionary *merged = Job_dict.mutableCopy;
    [merged addEntriesFromDictionary: Package_dict];
    NSLog(@"Package Dict:%@",merged);
}

}

1 个答案:

答案 0 :(得分:0)

似乎在tableView:cellForRowAtIndexPath中:您正在检查arrayOfJobList以获取有关单元格是否应该具有复选标记的信息。但是,在tableView:didSelectRowAtIndexPath中:您没有在arrayOfJobList中存储有关它被选中的任何信息。

另外在旁注中,您在取消选择时从array_Job,array_JobSK,array_Package和array_PackageSK中移除最后一个项目,但是您不会检查取消选择的项目是否实际上是放入的最后一个项目阵列? 例如,如果您选择单元格项目A,B和C,取消选择A,您将从这些数组中删除C?