2012-10-23 26 views
5

Tôi đã thực hiện một tableView trong đó tôi cần phải chọn nhiều tùy chọn. Các tùy chọn được chọn nhưng khi tôi cuộn chế độ xem bảng, tùy chọn dấu kiểm sẽ biến mất và một số hàng khác hiển thị dấu kiểm đó. Đây là mã của tôi trong didselectedrowAtindex phương pháp table_optionUITableViewselectedcellsNSMutableArrayLoại phụ kiện UITableview biến mất khi di chuyển

[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]; 
    [email protected]"Select"; 
    // cell.accessoryType = UITableViewCellAccessoryNone; 

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

    [selectedCells addObject:rowNsNum]; 
    [email protected]"Selected"; 
    // cell.accessoryType = UITableViewCellAccessoryCheckmark; 

} 



[table_option reloadData]; 

pls help sớm

Trả lời

12

Bạn cần phải kiểm tra các tế bào đã được chọn hay không trong phương pháp cellForRowAtIndexPath. Vấn đề này đang xảy ra vì tableView tái sử dụng các ô. Vui lòng viết mã bên dưới theo phương pháp cellForRowAtIndexPath của bạn, nó sẽ giải quyết vấn đề.

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

Awesome, cảm ơn rất nhiều .. này giải quyết vấn đề ... nhờ một lần nữa – wasim

+1

Với niềm vui. Cảm ơn bạn đã bình luận. :) –

+0

"containsObject", đây là chìa khóa, giúp tôi tiết kiệm rất nhiều tác phẩm. –

0

Trong trường hợp của tôi chỉ thiết lập accessoryType = UITableViewCellAccessoryCheckmark trong cellForRowAtIndexPath đã không làm việc.

Sự cố xảy ra khi UITableView đạt đến phần tử đầu tiên và nếu bạn cố gắng cuộn nhiều hơn, các ô hiển thị sẽ ngừng hoạt động và dấu kiểm của chúng sẽ biến mất.

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row]; 

[cell setAccessoryType:UITableViewCellAccessoryNone];// tricky part is this line 

if ([selectedCells containsObject:rowNsNum] ) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
0

Kiểm tra ví dụ này

cell.accessoryType = UITableViewCellAccessoryNone; 
for (int x = 0; x < selectedIds.count; x++) { 
    if ([[selectedIds objectAtIndex:x] isEqualToString:[[source objectAtIndex:[indexPath row]] objectForKey:@"id"]]) 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
Các vấn đề liên quan