2012-08-30 18 views
8

का चयन करने से अलग है मेरे पास UITableview है कि मैं कुछ कार्य प्रदर्शित कर रहा हूं और प्रत्येक पंक्ति में कार्यों को पूरा करने के लिए चेकबॉक्स है या नहीं।UITableViewCell AccessoryView का चयन, पंक्ति

जब मैं चेकबॉक्स पर टैप करता हूं तो चेकमार्क टॉगल करना चाहता हूं, और जब उपयोगकर्ता पंक्ति पर टैप करता है तो विस्तृत दृश्य पर स्विच करता है। बाद बस,

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

हालांकि, मैं चयन क्षेत्रों को अलग करना चाहता था का उपयोग करते समय accessoryview चयन किया जाता है केवल चेकबॉक्स टॉगल, और विस्तृत दृश्य तभी होता है जब सेल के बाकी चयन किया जाता है में जाकर, आसान है। यदि मैं accessoryview के अंदर UIbutton जोड़ता हूं, तो उपयोगकर्ता पंक्ति और UIButton का चयन करेगा जब वे केवल चेकबॉक्स को हिट करना चाहते थे, है ना?

इसके अलावा, क्या होगा यदि उपयोगकर्ता accessoryview के साथ खींचकर टेबलव्यू के माध्यम से स्क्रॉल कर रहा था? क्या यह टचअप पर UIButton पर कोई कार्रवाई नहीं करेगा?

किसी के पास यह कैसे करना है इस पर कोई विचार है? आपके समय के लिए धन्यवाद!

उत्तर

16

कैसे इस प्रतिनिधि विधि के अंदर गौण नल प्रबंधन के बारे में:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 

संपादित करें:

आपको लगता है कि accessoryButtonTappedForRowWithIndexPath: विधि का जवाब कस्टम accessoryView के लिए कुछ इस तरह कर सकते हैं।

cellForRowAtIndexPath: विधि में -

BOOL checked = [[item objectForKey:@"checked"] boolValue]; 
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"]; 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
button.frame = frame; 
[button setBackgroundImage:image forState:UIControlStateNormal]; 

[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; 
button.backgroundColor = [UIColor clearColor]; 
cell.accessoryView = button; 

- (void)checkButtonTapped:(id)sender event:(id)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition]; 
    if (indexPath != nil) 
    { 
    [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath]; 
    } 
} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row]; 
    BOOL checked = [[item objectForKey:@"checked"] boolValue]; 
    [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"]; 

    UITableViewCell *cell = [item objectForKey:@"cell"]; 
    UIButton *button = (UIButton *)cell.accessoryView; 

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"]; 
    [button setBackgroundImage:newImage forState:UIControlStateNormal]; 
} 
+1

कैसे आप अपने कस्टम सहायक बटन वापस तालिका दृश्य के प्रतिनिधि के लिए कॉल करने के लिए मिलता है? –

+0

वाह, मुझे नहीं पता कि मुझे यह कैसे याद आया, बेवकूफ सवाल के लिए खेद है और मदद के लिए धन्यवाद! मैं इसे एक शॉट दूंगा, ऐसा लगता है कि मैं यही देख रहा था। – Cody

+1

@CarlVeazey अच्छा बिंदु, प्रलेखन का कहना है कि accessoryButtonTappedForRowWithIndexPath प्रकटीकरण बटन एक्सेसरी प्रकार का जवाब देता है, जो कि मैं टाइपबॉक्स नहीं चाहता क्योंकि मैं उस प्रकार का उपयोग नहीं करना चाहता हूं। मैं विस्तृत दृश्य के लिए प्रकटीकरण बटन का उपयोग करके और पंक्ति में कहीं और एक चेकमार्क टॉगल करने के लिए शेष पंक्ति को टैप करके प्राप्त कर सकता हूं, लेकिन यह वही नहीं है जो मैं चाहता था। – Cody

संबंधित मुद्दे