yerine didSelectRowAtIndexPath:
tüm hücrelerin .accessoryType
değiştirme, ben sadece bu ile yani
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
self.selectedIndexPath = indexPath;
[tableView reloadData];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
...
cell.accessoryType = [indexPath compare:self.selectedIndexPath] == NSOrderedSame
? UITableViewCellAccessoryCheckmark
: UITableViewCellAccessoryNone;
...
}
, bazı Ivar seçilen endeks depolanması ve veri kaynağının -tableView:cellForRowAtIndexPath:
yönteminde .accessoryType
değiştirmek önermek Görünür hücreler etkilenecek ve ekran dışındaki milyonlarca hücrenin değiştirilmesi gerekmeyecek. Eğer uygun biçimde
Çok doğru, burada bir hücreyi seçerek genel durumda Swift tam uygulanması var .. sınıfta başka yerde selectedIndexPath kullanmayı tercih ediyorum. Örneğin, uygun hücre prototipini seçmek için cellForRowAtIndexPath
.
// SelectingTableViewController
import UIKit
class SelectingTableViewController: UITableViewController
{
internal var selectedIndexPath:NSIndexPath? = nil
override func viewDidLoad()
{
super.viewDidLoad()
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension
self.clearsSelectionOnViewWillAppear = false;
}
override func tableView
(tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath)
{
print("did select....")
// in fact, was this very row selected,
// and the user is clicking to deselect it...
// if you don't want "click a selected row to deselect"
// then on't include this clause.
if selectedIndexPath == indexPath
{
print("(user clicked on selected to deselect)")
selectedIndexPath = nil
tableView.reloadRowsAtIndexPaths(
[indexPath],
withRowAnimation:UITableViewRowAnimation.None)
tableView.deselectRowAtIndexPath(indexPath, animated:false)
return
}
// in fact, was some other row selected??
// user is changing to this row? if so, also deselect that row
if selectedIndexPath != nil
{
let pleaseRedrawMe = selectedIndexPath!
// (note that it will be drawn un-selected
// since we're chaging the 'selectedIndexPath' global)
selectedIndexPath = indexPath
tableView.reloadRowsAtIndexPaths(
[pleaseRedrawMe, indexPath],
withRowAnimation:UITableViewRowAnimation.None)
return;
}
// no previous selection.
// simply select that new one the user just touched.
// note that you can not use Apple's willDeselectRowAtIndexPath
// functions ... because they are freaky
selectedIndexPath = indexPath
tableView.reloadRowsAtIndexPaths(
[indexPath],
withRowAnimation:UITableViewRowAnimation.None)
}
}
:
Ve cellForRowAtIndexPath yöntemlerinde
i aşağıdaki kodu gibi bir şey var. Sadece onay işaretini (ve tabii ki yeni kontrol edilen hücre haline gelen hücreyi) içeren bir hücreyi değiştirin. –