objective-c - 滚动表格 View 时未选中复选标记

标签 objective-c ios uitableview

我正在使用 tableview 和显示数据。在表中查看所有已发送邮件的 checkMark 项目。但是当滚动表格时,复选标记未被选中。我不想在滚动表格时取消选中。请帮我。我正在开发以下代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier ";
    CaptureCell *cell = (CaptureCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    if (cell == nil) { 
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CaptureCell"
                                                     owner:self options:nil];
        for (id oneObject in nib) if ([oneObject isKindOfClass:[CaptureCell class]])
            cell = (CaptureCell *)oneObject;
    } 

    if ([[[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]isEqualToString:[dict objectForKey:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]]])
    {
        cell.confirmMail.image = [UIImage imageNamed:@"mailConfirm.png"];
    }
    NSString *input;
    input = [[dataArray objectAtIndex:indexPath.row]objectForKey:@"amount"];
    [input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    //NSLog(@"after removing white spaces %@",input);
    double price;
    NSString *numberAsString = nil;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [numberFormatter setLocale:gbLocale];
    [gbLocale release];
    [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    if ([[input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]length]!=0)
    {
        NSLog(@"the input is***** %@",input);           
        price=[[input stringByReplacingOccurrencesOfString:@"$" withString:@""] doubleValue];
        NSLog(@"the price is %f",price);
        numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:price]];
        NSLog(@"the numberAsString...... %@",numberAsString);
    }
    [numberFormatter release];


    cell.eventNameLbl.text = [[dataArray objectAtIndex:indexPath.row]objectForKey:@"expenseType"];
    cell.expenTypeLbl.text = [[dataArray  objectAtIndex:indexPath.row]objectForKey:@"empName"];
    cell.dataLbl.text = [[dataArray objectAtIndex:indexPath.row]objectForKey:@"Date"];
    cell.amountLbl.text = numberAsString;

    cell.billImage.image = [UIImage imageWithData:[[dataArray objectAtIndex:indexPath.row]objectForKey:@"image"]];

    //[cell.textLabel setText:[NSString stringWithFormat:@"Row %d", indexPath.row]];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSIndexPath* selection = [tableView indexPathForSelectedRow];
    if (selection && selection.row == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;  
    }



    // if([[[captureArray objectAtIndex:indexPath.row]objectForKey:@"eventName"]isEqualToString:[[selectedData objectAtIndex:indexPath.row]objectForKey:@"eventName"]])
    //  NSLog(@"Equal");

    //[cell setEditing:YES animated:YES];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *selected = [dataArray objectAtIndex:indexPath.row];


    if([tableView isEditing] == YES)
    {
        AppDelegate *sharedDelegate = [[UIApplication sharedApplication]delegate];
        sharedDelegate.isEditCapture = YES;
        sharedDelegate.findDate = YES;
        NSString *imgDataStr = [[NSString alloc]initWithData:[[dataArray objectAtIndex:indexPath.row]objectForKey:@"image"] encoding:NSUTF8StringEncoding];
        NSLog(@"imageStr%@",imgDataStr);
        ExpensesCaptureViewController_iPhone *nextController = [[ExpensesCaptureViewController_iPhone alloc] initWithNibName:@"ExpensesCaptureViewController_iPhone" bundle:nil];

        nextController.captureId = [[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"];
        [self.navigationController pushViewController:nextController animated:YES];
        [imgDataStr release];
        [nextController release];
        if([selectedData count]>0)
        {
            [selectedData removeObject:selected];
        }

    }


    else {
        if ([[[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]isEqualToString:[dict objectForKey:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]]])
        {
            NSLog(@"sent items");
        }

        else {
            if (cell.accessoryType == UITableViewCellAccessoryNone)
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;

                [selectedData addObject:selected];

            }

            else 
            {

                cell.accessoryType = UITableViewCellAccessoryNone;            

                [selectedData removeObject:selected];   

            }

        }


    }

    NSLog(@"DataObjects %d %@",[selectedData count] ,[[dataArray objectAtIndex:indexPath.row] objectForKey:@"eventName"]);  

    //[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

}

最佳答案

当调用 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法并设置时滚动 tableView 的问题

NSIndexPath* selection = [tableView indexPathForSelectedRow];
    if (selection && selection.row == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;  
    }

这意味着当您滚动时,indexPath 将仅针对当前行,因此当前行的复选标记仍将被选中,但所有其他行将进入 else 条件并且复选标记将被删除。

要修复它,您应该执行以下操作:

定义以下属性:

@property (nonatomic)BOOL isViewIsLoadedForTheFirstTime;

-(void)viewDidLoad
{
    self.isViewIsLoadedForTheFirstTime = YES;
}

然后在 (cellForRow) 方法中替换这些行

if (selection && selection.row == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;  
    }

到以下几行:

if (self.isViewIsLoadedForTheFirstTime) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
        self.isViewIsLoadedForTheFirstTime = NO;
    } 

这将使您仅在第一次加载 View 时设置复选标记,当您滚动时它不会改变。

希望对你有帮助

关于objective-c - 滚动表格 View 时未选中复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13395306/

相关文章:

iphone - 在没有重新加载数据的情况下强制在表中进行屏幕更新

objective-c - 如何将文件映射到 OSX 中的虚拟内存管理器?

ios - 单个 UIView 上的多个同时 UIViewAnimations

iphone - 仅在第一次打开时加载 ModalView

ios - 一个带有 6 个选项卡的 TabBar,一个 ViewController,一个 WebView

ios - TableView Cell with Minor Lag (Async Proper Use?) Swift

objective-c - 获取参数异常(无 URL 参数)!

objective-c - 隐藏 Mac OS X Launchpad 上的图标而不修改 DB?

ios - IOS 中的 Facebook 登录可以在模拟器上使用,但不能在安装了 native 应用程序的设备上使用

ios - 创建 View Controller 数组?