Note that there are some explanatory texts on larger screens.

plurals
  1. POTableview [cell.contentview viewwithtag:] is returning nil
    text
    copied!<p>I have a tableview with four sections first section has 7 rows in that first six rows has a textfield and the last row has two textfields </p> <p>Section 1</p> <p>t1 <br> t1 <br> t1 <br> t1<br> t1<br> t1<br> t1 t2<br> </p> <p>section 2</p> <p>t1 t2<br> t1 t2 // second row textfields are placed in fourth rows <br> t1 t2<br> t1 t2 // fourth row textfields are placed in someother rows<br> t1 t2<br> t1 t2<br> t1 t2<br> </p> <p>section 3</p> <p>t1 t2<br> t1 t2<br> t1 t2<br> t1 t2<br> t1 t2<br> t1 t2<br> t1 t2<br> <br> section 4</p> <p>t1<br> t1<br> t1<br> t1<br> t1<br> t1<br> t1<br> t1<br> </p> <p>Second &amp; third section contains seven rows having two textfields each</p> <p>Fourth section has eight rows containing one textfield in each row I have assigned unique tags to all the textfields</p> <p>In textFieldDidEndEditing I am saving the contents to an array in appropriate index.</p> <p>If I am scrolling the tableview after entering data in the textfield. Some textfield positions are changed (i.e.) textfield in the second row are placed in the third row similarly some rows are swapped.</p> <p>I have created 5 cell identifiers one for the first six rows in section one, second for the last row in the first section and remaining three cell identifiers for the other sections.</p> <p>When I scroll some times cell.contentview viewWithtag returns nil value for tag</p> <p>I think this is the reason for wrong textfield in some rows.</p> <p>How to rectify this problem? </p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier;&lt;bn&gt; UITableViewCellStyle style = UITableViewCellStyleDefault;&lt;br&gt; BOOL selectable = NO;&lt;br&gt; switch (indexPath.section) { case 0: { if(indexPath.row &lt; 6) identifier = @"firstsectionSixRows"; else identifier = @"firstsectionLastRow"; break; } case 1: identifier = @"secondsection"; break; case 2: identifier = @"thirdsection"; break; case 3: identifier = @"fourthsection"; break; } return [self createCellForIdentifier:identifier tableView:tableView indexPath:indexPath style:style selectable:selectable]; } </code></pre> <p>In createCellForIdentifier method</p> <pre><code>else if([identifier isEqualToString:@"secondsection"]) { int TextTag = 8 + indexPath.row * 2; UILabel *lblName = (UILabel*)[cell.contentView viewWithTag:10000]; lblName.text = [NSString stringWithFormat:@"G%d",indexPath.row + 1]; //NSLog(@"row = %d texttag = %d",indexPath.row,TextTag); UITextField *txtFirst = (UITextField*) [cell.contentView viewWithTag:TextTag]; if([textFieldArray count] &gt; 0) { if(![[textFieldArray objectAtIndex:TextTag] isEqualToString:@""]) { if(txtFirst) txtFirst.text = [NSString stringWithFormat:@"%@",[textFieldArray objectAtIndex:TextTag]]; else NSLog(@"Textfield is nil \n tag = %d \n cell section = %d row = %d",TextTag,indexPath.section,indexPath.row); } } UITextField *txtSecond = (UITextField*) [cell.contentView viewWithTag:TextTag + 1]; if([textFieldArray count] &gt; 0) { if(![[textFieldArray objectAtIndex:TextTag + 1] isEqualToString:@""]) { if(txtSecond) txtSecond.text = [NSString stringWithFormat:@"%@",[textFieldArray objectAtIndex:TextTag + 1]]; else NSLog(@"Textfield is nil \n tag = %d \n cell section = %d row = %d",TextTag + 1,indexPath.section,indexPath.row); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload