IOS advanced development -- CollectionView: Modify cell text and model reconstruction

Source: Internet
Author: User

IOS advanced development -- CollectionView: Modify cell text and model reconstruction

This blog is based on "iOS advanced development-CollectionView dynamic addition, deletion, cell, and model reconstruction. In the previous blog, we added and deleted cells dynamically and used the Model for restructuring. What we want to achieve today is to dynamically modify the description text in the cell. Through this case, we can find the advantages of using Model. The Code has been uploaded to "dynamically add cell and section implementations" in: https://github.com/chenyufeng1991/CollectionView ".

(1) Here I will press cell to display the input dialog box and enter the modified description text. CollectionView itself does not have a long-pressed event. Here I add the long-pressed gesture. This function is also described in cell long-press event Implementation of CollectionView, iOS advanced development. The implementation code is as follows:

 

# Pragma mark-create a long-pressed gesture-(void) createLongPressGesture {// create a long-pressed gesture listener listen * longPress = [[using alloc] initWithTarget: self action: @ selector longPress. minimumPressDuration = 1.0; // Add the long-pressed gesture to the view for implementing the long-pressed operation [self. collectionView addGestureRecognizer: longPress];}

(2) then, you need to pop up the input dialog box in the long-start method, and modify the text when you click the OK button in the dialog box. The implementation code is as follows:

 

 

-(Void) myHandleTableviewCellLongPressed :( UILongPressGestureRecognizer *) gestureRecognizer {CGPoint pointTouch = [gestureRecognizer locationInView: self. collectionView]; if (gestureRecognizer. state = UIGestureRecognizerStateBegan) {NSLog (@ starting with long-press gesture); NSIndexPath * indexPath = [self. collectionView indexPathForItemAtPoint: pointTouch]; if (indexPath = nil) {NSLog (@ blank);} else {UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @ prompt message: @ enter the modified description preferredStyle: UIAlertControllerStyleAlert]; // you can enter the text in the prompt box using the following methods: [alertController addAction: [UIAlertAction actionWithTitle: @ confirm style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _ Nonnull action) {UITextField * cellDescTextField = alertController. textFields. firstObject; NSString * cellDesc = cellDescTextField. text; NSLog (@ the input text is: % @, cellDesc); // locate the section of the current operation; SectionModel * section = [self. dataSectionArray objectAtIndex: indexPath. section]; // locate the cell of the current operation; CellModel * cell = [section. cellArray objectAtIndex: indexPath. row]; // modify the description of the cell. cellDesc = cellDesc; // determine the current cell array; self. dataCellArray = section. cellArray; // Replace the content in the cell array; [self. dataCellArray replaceObjectAtIndex: indexPath. row withObject: cell]; // update interface; [self. collectionView reloadData];}]; [alertController addAction: [UIAlertAction actionWithTitle: @ cancel style: Cancel handler: nil]; [alertController identifier: ^ (UITextField * _ Nonnull textField) {textField. placeholder = @ enter the Section name;}]; [self presentViewController: alertController animated: true completion: nil]; NSLog (@ Section = % ld, Row = % ld, (long) indexPath. section, (long) indexPath. row) ;}} if (gestureRecognizer. state = UIGestureRecognizerStateChanged) {NSLog (@ );} if (gestureRecognizer. state = UIGestureRecognizerStateEnded) {NSLog (@ long-press gesture end );}}

 

 

(3) The implementation effect is as follows:


.

 

 

.

 

In summary, I will continue to update the use of CollectionView and implement other complex and practical effects. Please keep an eye on it.

 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.