0

设置:我有一个名为“_itemListArray(ivar)”的属性,它设置为“Item(NSString itemName, NSString itemPrice)”列表。我用这些项目填充 UITableView,用户可以选择多行,并在该行上显示一个复选标记。选中单元格的 indexPath 存储到 IVAR(_selectedItemRows)。如果用户再次选择该行,则复选标记附件设置为无,并且 indexPath 从 IVAR(_selectedItemRows) 中删除。在“cellForRowAtIndexPath”中,我根据 _selectedItemRows(已检查单元格的 indexPaths 数组)中的所有 indexPaths 检查当前排队的 indexPath。如果索引路径在数组中,我检查出队的单元格,如果不是,我取消选中它。

问题:复选标记附件设置正确(didSelectRowAtIndexPath),但是当我滚动时,它的行为很时髦。例如,如果我检查第一个单元格然后向下滚动,然后向上滚动回第一个单元格,nslogs 已验证我的程序知道检查该单元格,但它似乎没有。
此外,如果我检查 2 个或更多单元格,请向下滚动,然后向上滚动,通常最后一个单元格是唯一检查的。

代码

@implementation  
@synthesize itemListArray = _itemListArray;  
@synthesize selectedItemRows = _selectedItemRows;  
-(void)setItemListArray:(NSArray *)itemListArray  
{  
    _itemListArray = itemListArray;  
    [_propTableView reloadData];  
}  
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    _selectedItemRows = [[NSMutableArray alloc] init];  
}  
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
{  
    // Return the number of rows in the section.  
    return [_itemListArray count];  
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    static NSString *CellIdentifier = @"Item Selected Reuse"; //Identifier of prototype cell  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     

    if (nil == cell) { //If somethong goes wrong, all hell breaks loose.  
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
         NSLog(@"%s", __PRETTY_FUNCTION__);  
    }  
    // Configure the cell...  
    Item *curItem = [_itemListArray objectAtIndex:indexPath.row]; //Get the model information at row location.  
    cell.textLabel.text = curItem.itemName; //Set the name of the item in title field  
    cell.detailTextLabel.text = curItem.itemPrice; //Set the price of the item in the detail field.  
    for(NSIndexPath * elem in _selectedItemRows)
    { //Enumerate through checked cells  
        //NSIndexPath *ip = [_selectedItemRows objectAtIndex:x];  
        if ([indexPath compare:elem] == NSOrderedSame) { //If the current cell index path ='s any index path in the array of checked cells, check this cell.  
            cell.accessoryType = UITableViewCellAccessoryCheckmark;   
        } else {  
            cell.accessoryType = UITableViewCellAccessoryNone;  
        }  
    }  
    return cell;  
}  
//pragma mark - Table view delegate  
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //Get cell clicked on.   
    if(cell.accessoryType == UITableViewCellAccessoryNone){ //When selected, if the cell is checked, uncheck it.  
        cell.accessoryType = UITableViewCellAccessoryCheckmark;  
        [_selectedItemRows addObject:indexPath]; //Add the index path of checked cell into array to use later for comparisons  
    } else {  
        if(cell.accessoryType == UITableViewCellAccessoryCheckmark){ //If the cell is checked, uncheck it when clicked on  
            cell.accessoryType = UITableViewCellAccessoryNone;  
            [_selectedItemRows removeObject:indexPath]; //Remove that index path of unchecked cell from index array  
        }  
    }  
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//Deselect row after done.  
}  
@end  
//Other code left out for brevity sake  

4

1 回答 1

1

您的代码中有逻辑错误。想想这段代码发生了什么:

for(NSIndexPath * elem in _selectedItemRows)
{ //Enumerate through checked cells  
    //NSIndexPath *ip = [_selectedItemRows objectAtIndex:x];  
    if ([indexPath compare:elem] == NSOrderedSame) { //If the current cell index path ='s any index path in the array of checked cells, check this cell.  
        cell.accessoryType = UITableViewCellAccessoryCheckmark;   
    } else {  
        cell.accessoryType = UITableViewCellAccessoryNone;  
    }  
}  

除非当前行的索引路径恰好是_selectedItemRows单元格中的最后一个,否则将清除复选标记。它会在找到它时设置复选标记,_selectedItemRows然后在继续搜索时取消设置它。相反,您想用以下内容替换它:

cell.accessoryType = UITableViewCellAccessoryNone; 
for(NSIndexPath * elem in _selectedItemRows)
{ //Enumerate through checked cells  
    //NSIndexPath *ip = [_selectedItemRows objectAtIndex:x];  
    if ([indexPath compare:elem] == NSOrderedSame) { //If the current cell index path ='s any index path in the array of checked cells, check this cell.  
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        break;
    }  
}  
于 2012-09-06T01:56:13.470 回答