UITableView cellForRowAtIndexPath复选标记附件设置表现得很奇怪

时间:2012-09-05 23:14:54

标签: objective-c uitableview didselectrowatindexpath accessory checkmark

设置:我有一个名为“_itemListArray(ivar)”的属性,它被设置为“Item(NSString itemName,NSString itemPrice)”列表。我用这些项填充UITableView,用户可以选择多行,在该行上显示一个复选标记。已检查单元格的indexPath存储到IVAR(_selectedItemRows)。如果用户再次选择该行,则将checkmark附件设置为none,并从IVAR(_selectedItemRows)中删除indexPath。在“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  

1 个答案:

答案 0 :(得分: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;
    }  
}