UITableview配件类型在滚动时消失 [英] UITableview accessory type disappear while scrolling

查看:119
本文介绍了UITableview配件类型在滚动时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个tableView,我需要选择多个选项。选项被选中,但是当我滚动表格视图时,复选标记选项消失,其他一些行显示该复选标记。
这是我的代码 didselectedrowAtindex 方法 table_option UITableView selectedcells NSMutableArray

I have made a tableView in which i need to select multiple options. The options are selected but when i scroll the table view the check mark option get disappear and some other rows displays that check mark. This is my code in didselectedrowAtindex method table_option is UITableView and selectedcells is NSMutableArray

[table_option deselectRowAtIndexPath:indexPath animated:YES];
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum]  )
{
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
        cell.accessoryType = UITableViewCellAccessoryNone;
    else 
        cell.accessoryType = UITableViewCellAccessoryCheckmark;



    [selectedCells removeObject:rowNsNum];
    sel.text=@"Select";
   // cell.accessoryType = UITableViewCellAccessoryNone;

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

    [selectedCells addObject:rowNsNum];
   sel.text=@"Selected";
   // cell.accessoryType = UITableViewCellAccessoryCheckmark;

}



[table_option reloadData];

请尽快帮助

推荐答案

您需要在 cellForRowAtIndexPath 方法中检查是否已选择单元格。出现此问题的原因是tableView重新使用单元格。
请在 cellForRowAtIndexPath 方法中写下以下代码,它将解决问题。

You need to check the cell is already selected or not in the cellForRowAtIndexPath method. This issue is happening because tableView re-uses the cells. Please write the below code in your cellForRowAtIndexPath method, it'll solve the issue.

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum]  )
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

这篇关于UITableview配件类型在滚动时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆