ios - Change Table View Cell's UIImage on Tap -


शायद एक साधारण तय।

मेरे तालिका दृश्य नियंत्रक के प्रत्येक कक्ष के अंदर मुझे एक छवि मिली और उस पर एक टैप इशारे पहचानकर्ता यह ठीक काम कर रहा है और उचित रूप से लॉगिंग कर रहा है।

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

यहाँ कैच है: क्या यह काम कर रहा था जब यह केवल वही सशर्त वक्तव्य की बात थी, जिसे सेक्टरोवअटइंडपैथ विधि में किया गया था, लेकिन जब से मैंने इशारे पहचानकर्ता को जोड़ा और imageTapped पद्धति को बनाया, तो मुझे इसका अनुवाद नहीं करना पड़ सकता है इसलिए, अन्यथा उपयोगी धागे जैसे मेरे लिए काम नहीं करते।

हमेशा की तरह धन्यवाद आप लोग सबसे अच्छे हैं।

यहां कोड है:

  - (UITableViewCell *) तालिकादृश्य: (UITableView *) तालिका दृश्य सेलफोररौअटइंडएपपथ: (NSIndexPath *) indexPath {स्थिर NSString * सेलआईडेंटिफायर = @ "प्लेससेल"; UITableViewCell * सेल = [tableView dequeueReusableCellWithIdentifier: For CellEdentifierIndexPath: indexPath]; यदि (सेल == नीला) {सेल = [[यूआईटीबल दृश्यकेल alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: @ "PlacesCell"]; Cell.selectionStyle = UITableViewCellSelectionStyleNone; } // छवि दृश्य बनाएँ cell.imageView.image = [UIImage imageNamed: @ "checkmark.png"]; // हावभाव पहचानकर्ता जोड़ें UITapGestureRecognizer * tapped = [[UITapGestureRecognizer alloc] initWithTarget: स्वयं कार्रवाई: @selector (imageTapped)]; टैप किया गया। NumberOfTapsRequired = 1; [Cell.imageView addGestureRecognizer: टेप]; Cell.imageView.userInteractionEnabled = हाँ; [सेल ऐड एस्यूब्यूव्यू: सेल। आईमेजदृश्य]; रिटर्न सेल; } // विधि का नियंत्रण क्या होता है जब सेल के UIImage पर टैप किया जाता है - (शून्य) छविटैप {UITableViewCell * cell; UIImage * imageDefault = [UIImage image नामांकित: @ "चेकमार्क पेज"]; UIImage * imageRed = [UIImage imageNamed: @ "चेकमार्क (लाल)। PNG"]; UIImage * imageGreen = [UIImage imageNamed: @ "चेकमार्क (हरा)। PNG"]; If (cell.imageView.image == imageDefault) {cell.imageView.image = imageGreen; Cell.selected = true; NSLog (@ "चयनित"); } और {cell.imageView.image = imageRed; Cell.selected = false; NSLog (@ "अचयनित"); }}  

मैंने आपके कोड को संशोधित किया है, इसे चेक करें

  - (UITableViewCell *) तालिकादृश्य: (UITableView *) तालिका दृश्य सेलफोराअटइंडपैथ: (NSIndexPath *) सूचकांकपथ {स्थिर NSString * CellIdentifier = @ "PlacesCell"; UITableViewCell * सेल = [tableView dequeueReusableCellWithIdentifier: For CellEdentifierIndexPath: indexPath]; यदि (सेल == नीला) {सेल = [[यूआईटीबल दृश्यकेल alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: @ "PlacesCell"]; Cell.selectionStyle = UITableViewCellSelectionStyleNone; } // छवि दृश्य बनाएँ cell.imageView.image = [UIImage imageNamed: @ "checkmark.png"]; // हावभाव पहचानकर्ता जोड़ें UITapGestureRecognizer * tapped = [[UITapGestureRecognizer alloc] initWithTarget: स्वयं कार्रवाई: @selector (imageTapped :)]; टैप किया गया। NumberOfTapsRequired = 1; [Cell.imageView addGestureRecognizer: टेप]; Cell.imageView.userInteractionEnabled = हाँ; // [सेल ऐड एस्यूब्यूव्यू: सेल। आईमेजदृश्य]; रिटर्न सेल; } // विधि का नियंत्रण तब होता है जब सेल के UIImage टैप किया जाता है - (शून्य) छविटैप: (UIGestureRecognizer *) इशारा {UIImageView * selectedImageView = (UIImageView *) [इशारा दृश्य]; UIImage * imageDefault = [UIImage image नामांकित: @ "चेकमार्क पेज"]; UIImage * imageRed = [UIImage imageNamed: @ "चेकमार्क (लाल)। PNG"]; UIImage * imageGreen = [UIImage imageNamed: @ "चेकमार्क (हरा)। PNG"]; यदि (selectedImageView.image == imageDefault) {selectedImageView.image = imageGreen; //cell.selected = true; NSLog (@ "चयनित"); } अन्य {selectedImageView.image = imageRed; //cell.selected = false; NSLog (@ "अचयनित"); }}  

Comments

Popular posts from this blog

eclipse plugin - Run java code error: Workspace is closed -

ios - How do I use CFArrayRef in Swift? -

scala - Play Framework - how to bind form to a session field -