iOS進階開發——CollectionView修改cell的文本及模型重構

來源:互聯網
上載者:User

iOS進階開發——CollectionView修改cell的文本及模型重構

該篇部落格是在《iOS進階開發——CollectionView的動態增刪cell及模型重構》的基礎上繼續進行開發的。在之前那篇部落格中,我們實現了動態增刪cell,並且使用了模型Model進行重構。今天我們要實現的是動態修改cell中的標題文字,通過這個案例,我們能發現使用Model的好處。代碼已經上傳至:https://github.com/chenyufeng1991/CollectionView .中的“動態增加cell和section實現” 。

(1)我這裡通過長按cell的操作,彈出輸入對話方塊,然後輸入修改的標題文字。CollectionView本身沒有長按事件,我這裡通過增加長按手勢來實現。該功能在《iOS進階開發——CollectionView的cell長按事件實現》中也有說明。實現代碼如下:

 

#pragma mark - 建立長按手勢- (void)createLongPressGesture{    //建立長按手勢監聽  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]                                             initWithTarget:self                                             action:@selector(myHandleTableviewCellLongPressed:)];  longPress.minimumPressDuration = 1.0;  //將長按手勢添加到需要實現長按操作的視圖裡  [self.collectionView addGestureRecognizer:longPress];}

(2)然後需要在長按開始的方法內彈出輸入對話方塊,在點擊對話方塊確定按鈕的時候進行修改文本。實現代碼如下:

 

 

- (void) myHandleTableviewCellLongPressed:(UILongPressGestureRecognizer *)gestureRecognizer {      CGPoint pointTouch = [gestureRecognizer locationInView:self.collectionView];    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {    NSLog(@長按手勢開始);        NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pointTouch];    if (indexPath == nil) {      NSLog(@空);    }else{      UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@提示 message:@請輸入修改後的標題文字 preferredStyle:UIAlertControllerStyleAlert];      //以下方法就可以實現在提示框中輸入文本;      [alertController addAction:[UIAlertAction actionWithTitle:@確定 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        UITextField *cellDescTextField = alertController.textFields.firstObject;                NSString *cellDesc = cellDescTextField.text;        NSLog(@輸入的文字是:%@,cellDesc);                        //找到當前操作的section;        SectionModel *section = [self.dataSectionArray objectAtIndex:indexPath.section];        //找到當前操作的cell;        CellModel *cell = [section.cellArray objectAtIndex:indexPath.row];        //修改該cell的標題文字;        cell.cellDesc = cellDesc;                //確定當前的cell數組;        self.dataCellArray = section.cellArray;        //替換cell數組中的內容;        [self.dataCellArray replaceObjectAtIndex:indexPath.row withObject:cell];        //更新介面;        [self.collectionView reloadData];                      }]];            [alertController addAction:[UIAlertAction actionWithTitle:@取消 style:UIAlertActionStyleDefault handler:nil]];      [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        textField.placeholder = @請輸入Section名稱;      }];      [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(@長按手勢結束);  }}

 

 

(3)實現效果如下:


 

 

 

總結,我會持續對CollectionView的使用進行更新,並實現其他複雜實用的效果。請大家不斷關注。

 

 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.