2011-04-04 23 views
10

UITableViewCellAccessoryCheckmark kullanarak bir onay kutusu içeren her satırda bir UITableView var. didSelectRowAtIndexPath yöntemini kullanarak tüm onay kutularının işaretini nasıl kaldıracağımı anlayamıyorum.Tüm satırların işaretini kaldırmak için UITableViewCellAccessoryCheckmark

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *oldCell; 

    int count = [self.myTableRowNamesArray count]; 

    for (NSUInteger i = 0; i < count; ++i) {         
     // Uncheck all checkboxes 
     // OF COURSE THIS DOESN'T WORK 
     // BECAUSE i IS AN INTEGER AND INDEXPATH IS A POINTER 
     FOO: oldCell = [myTableView cellForRowAtIndexPath:(int)i]; 
     // GOOD CODE: 
     oldCell = [penanceOptionsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:indexPath]; 
    newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

cevap

4

Evet, cellForRowAtIndexPath: yerine tamsayı NSIndexPath yüzden sadece üst üste i geçmek ve bölüm için 0 sizin döngü gayet sonra bir bölümünü kullanıyorsanız

indexPathForRow:inSection: 

kullanarak indexPath yapmak kullanır.

11
for (UITableViewCell *cell in [myTableView visibleCells]) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

Gerçekten de, yalnızca onay işareti kümesine sahip olan tek bir hücreyi değiştirmek daha iyi olur. Bu bilgiyi modelinizde bir yerde saklamanız gerekiyor.

+1

:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 1. first unsetting the property [object someProperty:nil]; // 2. call the reloadData method to uncheck all the checkmarks [tableView reloadData]; // 3. check the selected cell UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // 4. set the checked property [object setSomeProperty:[indexpath row]]; } 

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. –

20

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) 

      } 

    } 
+1

, [reloadData] öğesini birden çok kez çağırıyorsa, herhangi bir kötü etki yaratıyor mu? – onmyway133

+0

top markotop (y) – tesmojones

5

Muhtemelen bu yöntemle mülkiyet çeşit belirliyoruz. Yani ben ne olduğunu: Demek istediğim bu That

if([object someProperty] == [indexpath row]){ 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark];   
    } else { 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 
İlgili konular