2010-09-08 8 views
11

कुछ समय पहले किसी ने पहले ही इस प्रश्न से पूछा था और कुछ उत्तरों दिए गए थे, लेकिन मैं वास्तव में उनमें से किसी को भी समझ नहीं पाया। तो मैं सोच रहा था कि अगर किसी को भी कैसे नीचे छवि पर दिखाया काम करने के लिए पर ट्यूटोरियल को समझने के लिए एक आसान लिखें सकता है:UITableViewCell में चेकबॉक्स कैसे जोड़ें ??

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

मैं बहुत आभारी होंगे अगर कोई यह वास्तव में कैसे क्योंकि इस को साझा कर सकते हैं वास्तव में अच्छा लग रहा है और मुझे अपने आवेदन में कुछ समान उपयोग करना अच्छा लगेगा :-)!

+0

इस लिंक http://walkingsmarts.com/creating-radio-button-and पढ़ -checkbox-listings-use-uitableview/यह आपकी मदद कर सकता है –

उत्तर

30

एक अनियंत्रित और जाँच की छवि बनाने ..

- (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 UITableView पर एक मान्यता प्राप्त एक्सेसर नहीं है ... मैं इसे कैसे काम कर सकता हूं? – quantum

+0

क्षमा करें कि indexPathForRowAtPoint होना चाहिए: .. कोड अब – LarsJK

+0

एचएम सही है, यह अभी भी cell.imageView पर किसी भी विशिष्ट नल को पहचानने के लिए प्रतीत नहीं होता है। कोई विचार? – quantum

1

एक तरीका जिस तरह से मैं सोच सकता हूं कि आप कस्टम UITableViewCell (this tutorial प्रारंभ करने के लिए अच्छा है। यह another, similar one है)। कस्टम UITableViewCell के अंदर आपको एक सर्कल छवि के साथ UIUutton डालने की आवश्यकता है। जब उपयोगकर्ता बटन का चयन करें, आप हरे रंग का गोला के लिए छवि को बदलने