'테이블뷰셀을'에 해당되는 글 1건

  1. 2010.10.12 테이블뷰와 테이블뷰셀을 수정하는 방법
아이폰어플개발정보2010. 10. 12. 04:01
테이블뷰와 테이블뷰셀을 수정하는 방법

First you have to create the UITableViewCell class that will contain your delegate function.

This delegate function is responsible for passing data back to your class ... for example, - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath function that you definitely have to know :) is a delegate method as well ... its passing the information abou what row you have selected back from the table view intor class.

Code for the EditFieldCell.h:

  1. //
  2. //  EditFieldCell.h
  3. //  iBlog
  4. //
  5. //  Created by Ondrej Rafaj on 12.11.09.
  6. //  Copyright 2009 Home. All rights reserved.
  7. //
  8. #import 
  9. @class EditFieldCell;  
  10. @protocol EditFieldCellDelegate   
  11. // we will make one function mandatory to include
  12. - (void)editDidFinish:(NSMutableDictionary *)result;  
  13. @optional  
  14. // and the other one is optional (this function has not been used in this tutorial)
  15. - (void)editStarted:(UITextField *)field;  
  16. @end  
  17. @interface EditFieldCell : UITableViewCell  {  
  18.     IBOutlet UITextField *textField;  
  19.     id  delegate;  
  20.     NSString *key;  
  21. }  
  22. @property (nonatomic, retain) IBOutlet UITextField *textField;  
  23. @property (nonatomic, assign) id  delegate;  
  24. @property (nonatomic, retain) NSString *key;  
  25. @end  

Code for the EditFieldCell.m:

  1. //
  2. //  EditFieldCell.m
  3. //  iBlog
  4. //
  5. //  Created by Ondrej Rafaj on 12.11.09.
  6. //  Copyright 2009 Home. All rights reserved.
  7. //
  8. #import "EditFieldCell.h"
  9. @implementation EditFieldCell  
  10. @synthesize textField, delegate, key;  
  11. - (void)textFieldDidBeginEditing:(UITextField *)tf {  
  12.     [[self delegate] editStarted:tf];  
  13. }  
  14. - (void)dealloc {  
  15.     [textField, key release];  
  16.     [super dealloc];  
  17. }  
  18. @end  

You have to of course create a new cell view in the Interface Builder and assign all the necessary variables onto your class :) ... I assume that you already know that stuff when you are reading a tutorial about delegate methods :))

and this is how to set the delegate in the main view:

  1. @interface EditBlogController : BasicTableController   

And we can't forget the function that will receive your data:

  1. - (void)editDidFinish:(NSMutableDictionary *)result {  
  2.     NSString *key = [result objectForKey:@"key"];  
  3.     NSString *value = [result objectForKey:@"text"];  
  4. }  

And finally here we have the cellForRowAtIndexPath function:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.     static NSString *CellIdentifier = @"MyCell";  
  3.     EditFieldCell *cell = (EditFieldCell *) [super.table dequeueReusableCellWithIdentifier:CellIdentifier];  
  4.     if (cell == nil){  
  5.         NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EditFieldCell" owner:nil options:nil];  
  6.         for(id currentObject in topLevelObjects) {  
  7.             if([currentObject isKindOfClass:[EditFieldCell class]]) {  
  8.                 cell = (EditFieldCell *) currentObject;  
  9.                 break;  
  10.             }  
  11.         }  
  12.     }  
  13.     int index = [indexPath indexAtPosition: [indexPath length] - 1];  
  14.     // next line is very important ... 
  15.     // you have to set the delegate from the cell back to this class
  16.     [cell setDelegate:self];  
  17.     // I am using the key to identify what field is user editing
  18.     NSString *key = @"key_for_your_value";  
  19.     NSString *placeholder = @"Your placeholder";  
  20.     cell.key = key;  
  21.     cell.textField.text = [blogData objectForKey:key];  
  22.     cell.textField.placeholder = placeholder;  
  23.     return cell;  
  24. }  

As I said before, I'm assuming that you already know how to create a table view and display data in it, but if you'll have any question please feel free to post a comment, I'll be happy to help you with that :).

Cheers,

Ondrej


We had to disable comments till the problem with spam will be solved, sorry for any inconvenience caused.

http://www.xprogress.com/post-41-editing-uitextview-in-uitableviewcell-via-delegate-method/

Posted by 오늘마감

댓글을 달아 주세요