Guest User

Untitled

a guest
Aug 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. Inserting row to the section in tableview dyanmically iphone
  2. -(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
  3. {
  4. NSLog(@"row in section::%i %i", section, sectionRows);
  5. if(section == 0)
  6. return 1;
  7. else if(section == 1)
  8. return 1;
  9. else
  10. return sectionRows;
  11.  
  12. }
  13.  
  14. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  15. {
  16. NSLog(@"cell rows in section%i %i", indexPath.section, [tableView numberOfRowsInSection:indexPath.section]);
  17. static NSString *MyIdentifier = @"MyIdentifier";
  18. MyIdentifier = @"TableView";
  19.  
  20. UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 31)];
  21. [backgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_accordian.png"]]];
  22.  
  23. CustomMyMedCellView *customCell;
  24. UIView *viewToHide;
  25.  
  26. if(indexPath.section == 0)
  27. {
  28. customCell = (CustomMyMedCellView *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];
  29. if (customCell == nil)
  30. {
  31. NSLog(@"in Section 1::::::");
  32. [[NSBundle mainBundle] loadNibNamed:@"CustomMyMedCellView" owner:self options:nil];
  33. customCell = customMyMedCellView;
  34. [customCell setTextFieldValue:@"Teva Generic Med" editable:NO];
  35. viewToHide= (UIView *)[customCell.contentView viewWithTag:4];
  36. viewToHide.hidden = YES;
  37. }
  38.  
  39.  
  40. }else if(indexPath.section == 1)
  41. {
  42. customCell = (CustomMyMedCellView *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];
  43. if(indexPath.row == 0)
  44. {
  45. if (customCell == nil)
  46. {
  47. [[NSBundle mainBundle] loadNibNamed:@"CustomMyMedCellView" owner:self options:nil];
  48. customCell = customMyMedCellView;
  49.  
  50. CGRect frame = customCell.textField.frame;
  51. frame.origin.x = 30.0;
  52. customCell.textField.frame = frame;
  53. [customCell setTextFieldValue:@"Accutane Capsules" editable:NO];
  54. viewToHide = (UIView *)[customCell.contentView viewWithTag:2];
  55. viewToHide.hidden = YES;
  56. }
  57. }
  58. }else
  59. {
  60. customCell = (CustomMyMedCellView *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];
  61. NSLog(@"in Section 3%@", customCell);
  62. if (customCell == nil)
  63. {
  64. NSLog(@"in Section 3 if");
  65. [[NSBundle mainBundle] loadNibNamed:@"CustomMyMedCellView" owner:self options:nil];
  66. customCell = customMyMedCellView;
  67. customMyMedCellView.textField.delegate = self;
  68. CGRect frame = customCell.textField.frame;
  69. frame.origin.x = 30.0;
  70. customCell.textField.frame = frame;
  71. [customCell setTextFieldValue:@"" editable:YES];
  72. viewToHide = (UIView *)[customCell.contentView viewWithTag:2];
  73. viewToHide.hidden = YES;
  74. viewToHide = (UIView *)[customCell.contentView viewWithTag:4];
  75. viewToHide.hidden = YES;
  76. viewToHide = (UIView *)[customCell.contentView viewWithTag:5];
  77. viewToHide.hidden = YES;
  78. viewToHide = (UIView *)[customCell.contentView viewWithTag:6];
  79. viewToHide.hidden = YES;
  80.  
  81. customCell.tag = indexPath.row;
  82.  
  83. [self.myMedsTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(sectionRows-1) inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  84.  
  85. }
  86.  
  87.  
  88. }
  89. if(!self.isEditClicked)
  90. {
  91. viewToHide = (UIView *)[customCell.contentView viewWithTag:1];
  92. viewToHide.hidden = YES;
  93. }else{
  94. viewToHide = (UIView *)[customCell.contentView viewWithTag:1];
  95. viewToHide.hidden = NO;
  96. }
  97. customCell.backgroundView = backgroundView;
  98. return customCell;
  99. }
  100.  
  101. - (void)addButtonClick:(id)sender {
  102. UIButton *clickedButton = (UIButton *) sender;
  103. if(clickedButton.tag == 2)
  104. {
  105. NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
  106. NSInteger rowsInSection = [self.myMedsTableView numberOfRowsInSection:2];
  107. if(rowsInSection>0){
  108. NSLog(@"%@", [[self.myMedsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowsInSection-1 inSection:2]] viewWithTag:rowsInSection-1 ]);
  109. CustomMyMedCellView *uiView = (CustomMyMedCellView *)[[self.myMedsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowsInSection-1 inSection:2]] viewWithTag:rowsInSection-1 ];
  110. // NSLog(@"%@", uiView.subviews);
  111. // UITextField *textField = (UITextField *)[uiView viewWithTag:3];
  112. if([uiView.textField .text length] > 0)
  113. {
  114. sectionRows+=1;
  115. }
  116. }else{
  117. sectionRows=1;
  118. }
  119. NSLog(@"Sectipn rows::::%i", sectionRows);
  120. for (NSInteger i = 0; i < sectionRows; i++) {
  121. NSLog(@"Sectipn rows:::: inside for");
  122. [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:2]];
  123. }
  124.  
  125. NSLog(@"%i",[indexPathsToInsert count]);
  126. [self.myMedsTableView beginUpdates];
  127. [self.myMedsTableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationBottom];
  128. [self.myMedsTableView endUpdates];
  129.  
  130. //[self.myMedsTableView reloadData];
  131.  
  132.  
  133.  
  134. }else{
  135. Search *tableViewController = [[Search alloc] initWithNibName:@"Search" bundle:[NSBundle mainBundle]];
  136. [self.navigationController pushViewController:tableViewController animated:YES];
  137. }
Add Comment
Please, Sign In to add comment