2009-08-12 7 views
0

多くのiPhoneアプリケーションでは、UITableViewControllerがチェックボックスリストとして使用されています。チェックボックスリストとしてUITableViewControllerを使用するときの既定項目の選択

これを自分自身で実装しようとしている間、私は項目をプログラムでデフォルト(つまり、デフォルトでは)にするために多くのフープを飛ばしなければなりませんでした。 、リストが表すものの現在の値)。私が思い付くことができました最高のは、私のビューコントローラクラスでviewDidAppearメソッドをオーバーライドすることである:

- (void)viewDidAppear:(BOOL)animated { 
    NSInteger row = 0; 

    // loop through my list of items to determine the row matching the current setting 
    for (NSString *item in statusItems) { 
     if ([item isEqualToString:currentStatus]) { 
      break; 
     } 
     ++row; 
    } 

    // fetch the array of visible cells, get cell matching my row and set the 
    // accessory type 
    NSArray *arr = [self.tableView visibleCells]; 
    NSIndexPath *ip = [self.tableView indexPathForCell:[arr objectAtIndex:row]]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:ip]; 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    self.lastIndexPath = ip; 

    [super viewDidAppear:animated]; 
} 

は、これがあれば、特定のセルとindexPathへの参照を取得するための最良の/のみ/最も簡単な方法です私はデフォルトで行をマークしたいですか?

答えて

1

ステータス項目を表示するには、とにかくtableView:cellForRowAtIndexPath:を実装する必要がありますか?それでは、なぜちょうどこのように、セルを返す前に、セルのアクセサリーの種類を設定していない:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // dequeue or create cell as usual 

    // get the status item (assuming you have a statusItems array, which appears in your sample code) 
    NSString* statusItem = [statusItems objectAtIndex:indexPath.row]; 

    cell.text = statusItem; 

    // set the appropriate accessory type 
    if([statusItem isEqualToString:currentStatus]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 

あなたのコードは非常に脆弱であり、あなたが[self.tableView visibleCells]を使用し、特にため。表示されている行より多くのステータス項目が画面に表示されている場合(名前が示すように、visibleCellsは現在表示されている表ビューのセルのみを返します)

+0

私はそのコードで馬鹿になっていると思った。遅い脳今日:P – Dana

関連する問題