2010-09-08 2 views
11

vor einiger Zeit hat jemand diese Frage schon gestellt und ein paar Antworten gegeben, aber ich habe sie nicht wirklich verstanden. Also habe ich mich gefragt, ob jemand könnte bitte einfach zu verstehen Tutorial schreiben, wie man die Dinge auf das Bild unten tun:Wie fügt man UITableViewCell Checkboxen hinzu ??

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

ich so dankbar wäre, wenn jemand genau teilen kann, wie man dies, weil es sieht wirklich cool aus und ich würde gerne etwas ähnliches in meiner Bewerbung verwenden :-)!

+0

diesen Link lesen http://walkingsmarts.com/creating-radio-button-and -checkbox-lists-using-uitableview/Es kann Ihnen helfen –

Antwort

30

ein unkontrolliertes Stellen und geprüft Bildes ..

- (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 ist kein anerkannter Accessor auf UITableView ... Wie bekomme ich es zum Funktionieren? – quantum

+0

Sorry, dass sollte indexPathForRowAtPoint sein: .. Code ist jetzt Korrigiert – LarsJK

+0

hm es scheint immer noch keine bestimmten Taps auf dem cell.imageView zu erkennen. irgendwelche Ideen? – quantum

1

Eine der Möglichkeiten, die ich denken kann, ist Sie benutzerdefinierte UITableViewCell (this tutorial ist gut für den Start. Dies ist another, similar one). In der benutzerdefinierten UITableViewCell müssen Sie dort eine UIButton mit einem Kreisbild platzieren. Wenn Benutzer auf die Schaltfläche auswählen, können Sie das Bild

auf den grünen Kreis ändern