설정 : "Item (NSString itemName, NSString itemPrice)"목록에 설정된 "_itemListArray (ivar)"속성이 있습니다. 이러한 항목으로 UITableView를 채우고 사용자는 여러 행을 선택하여 해당 행에 체크 표시를 표시 할 수 있습니다. 검사 된 셀의 indexPath는 IVAR (_selectedItemRows)에 저장됩니다. 사용자가 행을 다시 선택하면 체크 표시기 액세서리가 none으로 설정되고 indexPath는 IVAR (_selectedItemRows)에서 제거됩니다. "cellForRowAtIndexPath"에서 _selectedItemRows (체크 셀의 indexPath 배열)의 모든 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
나는 당신을 너무 사랑합니다. – jgvb