Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. - (IBAction)doneAdd:(UIBarButtonItem *)sender {
  2. [self.delegate addItem:[self newItem]];
  3. }
  4.  
  5. - (NSMutableArray *)newItem
  6. {
  7. NSMutableArray *newItem = [[NSMutableArray alloc] init];
  8.  
  9. for (int i = 0; i < [_appDelegate.title count]; i ++) {
  10. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
  11. UPFEditableUITableViewCell *cell = (UPFEditableUITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
  12. NSLog(@"%@", cell.editField.text);
  13. //[newItem addObject:cell.editField.text]; //this does not work as null cannot be added into a array
  14. }
  15. NSLog(@"%@", newItem);
  16. return newItem;
  17. }
  18.  
  19. #import "UPFEditableUITableViewCell.h"
  20.  
  21. @implementation UPFEditableUITableViewCell
  22.  
  23. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  24. {
  25. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  26. if (self) {
  27. self.editField = [[UITextField alloc] initWithFrame:CGRectZero];
  28. [self.contentView addSubview:self.editField];
  29. }
  30. return self;
  31. }
  32.  
  33. - (void)layoutSubviews
  34. {
  35. if ([self.detailTextLabel.text length] == 0) {
  36. self.detailTextLabel.text = @" ";
  37. }
  38.  
  39. [super layoutSubviews];
  40.  
  41. // place the edit field in the same place as the detail text field, give max width
  42. self.editField.frame = CGRectMake(self.detailTextLabel.frame.origin.x, self.detailTextLabel.frame.origin.y, self.contentView.frame.size.width-self.detailTextLabel.frame.origin.x, self.detailTextLabel.frame.size.height);
  43. }
  44.  
  45. - (void)showEditingField:(BOOL)show
  46. {
  47. self.detailTextLabel.hidden = YES;
  48. self.editField.text = self.detailTextLabel.text;
  49. }
  50.  
  51.  
  52. @end
  53.  
  54. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  55.  
  56. static NSString *cellReuseIdentifier = @"cellIdentifier";
  57.  
  58. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
  59.  
  60. if (!cell) {
  61. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
  62.  
  63. }else{
  64. NSLog(@"text is %@",cell.textLabel.text);
  65.  
  66. for (UIView *v in cell.contentView.subviews) {
  67. if ([v isKindOfClass:[UITextField class]]) {
  68. UITextField *textField = (UITextField *)v;
  69. [myDictionary setObject:textField.text forKey:indexPath]; // declare myDictionary in the interface first.This will also prevent the values from duplicating
  70. NSLog(@"%@",myDictionary);
  71. }
  72. }
  73. }
  74. return cell;
  75. }
  76.  
  77. [cell.editField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  78. cell.editField.delegate = self;
  79.  
  80. - (void)textFieldDidChange :(UITextField *)theTextField
  81. {
  82. NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
  83. [self.item removeObjectAtIndex:indexPath.row];
  84. [self.item insertObject:theTextField.text atIndex:indexPath.row];
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement