0

設置:我有一個名爲「_itemListArray(ivar)」,屬性設置爲「Item(NSString itemName,NSString itemPrice)」的列表屬性。我用這些項目填充UITableView,用戶可以選擇多行,在該行上顯示覆選標記。已檢查的單元格的indexPath存儲到IVAR(_selectedItemRows)。如果用戶再次選擇該行,checkmark附件將被設置爲none,並從IVAR中刪除indexPath(_selectedItemRows)。在「cellForRowAtIndexPath」中,我對照_selectedItemRows(已檢查單元格的indexPaths數組)中的所有indexPath檢查當前排隊的indexPath。如果索引路徑在數組中,我檢查已出隊的單元格,如果不是,則取消選中它。UITableView cellForRowAtIndexPath checkmark配置設置代理怪異

問題:檢查標記附件設置正確(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

你在你的代碼中的邏輯錯誤。想想這段代碼會發生什麼:

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; 
    } 
} 
+3

我愛你,太多了。 – jgvb