2010-09-08 15 views
11

jakiś czas temu ktoś już zadał to pytanie i kilka odpowiedzi zostało udzielonych, ale tak naprawdę nie rozumiem żadnego z nich. Więc zastanawiałem się, czy ktoś mógłby proszę pisać łatwym do zrozumienia tutorial jak zrobić rzeczy pokazane na obrazku poniżej:Jak dodać pola wyboru do UITableViewCell?

http://img208.yfrog.com/img208/6119/screenshotkmr.png

będę więc wdzięczny jeśli ktoś może podzielić się dokładnie jak to dlatego, że wygląda naprawdę fajnie i chciałbym użyć czegoś podobnego w mojej aplikacji :-)!

+0

przeczytać ten link http://walkingsmarts.com/creating-radio-button-and -checkbox-lists-using-uitableview/może ci pomóc –

Odpowiedz

30

Zrób niezaznaczone i sprawdzony obraz ..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if ([selectedRowsArray containsObject:[contentArray objectAtIndex:indexPath.row]]) { 
     cell.imageView.image = [UIImage imageNamed:@"checked.png"]; 
    } 
    else { 
     cell.imageView.image = [UIImage imageNamed:@"unchecked.png"]; 
    } 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)]; 
    [cell.imageView addGestureRecognizer:tap]; 
    cell.imageView.userInteractionEnabled = YES; //added based on @John 's comment 
    //[tap release]; 

    cell.textLabel.text = [contentArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer { 
    CGPoint tapLocation = [tapRecognizer locationInView:self.tableView]; 
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation]; 

    if ([selectedRowsArray containsObject:[contentArray objectAtIndex:tappedIndexPath.row]]) { 
     [selectedRowsArray removeObject:[contentArray objectAtIndex:tappedIndexPath.row]]; 
    } 
    else { 
     [selectedRowsArray addObject:[contentArray objectAtIndex:tappedIndexPath.row]]; 
    } 
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade]; 
} 
+0

indexForRowAtPoint nie jest rozpoznanym narzędziem dostępnym w UITableView ... jak mogę go uruchomić? – quantum

+0

Przepraszam, że powinien to być indexPathForRowAtPoint: .. Kod jest teraz poprawiony – LarsJK

+0

hm nadal nie rozpoznaje żadnych konkretnych kliknięć w komórce.imageView. jakieś pomysły? – quantum

1

Jednym ze sposobów, w jaki mogę wymyślić, jest niestandardowe UITableViewCell (this tutorial jest dobry do rozpoczęcia.To jest another, similar one). Wewnątrz niestandardowego UITableViewCell musisz umieścić UIButton tam z obrazem koła. Kiedy użytkownik wybraniu przycisku powoduje zmianę obrazu z zielonym kółkiem

Powiązane problemy