0

I am making a table object which by a swipe gesture changes the displayed values to completely different ones. This works because there is a UIPageControl which shows which set must be displayed on the table. The code involves a mutable array containing mutable arrays at the index of the page. For example the first page gives the mutable array at index 0. These mutable arrays contained, contain the table objects and give out the appropriate value to the table by referring to the index of the cell. What happens however is that when the table contains more than 1 cells, it mixes the objects of arrays of different pages, something that shouldn't happen as these are different arrays. The code is here:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    startDtLbl = [[UITextField alloc] initWithFrame:CGRectMake(10, 11, _answerTable.frame.size.width, 25)];
    startDtLbl.backgroundColor = [UIColor clearColor];
    startDtLbl.tag = 1;
    startDtLbl.returnKeyType = UIReturnKeyDone;
    startDtLbl.delegate = self
    [cell.contentView addSubview:startDtLbl];
}
else {
    startDtLbl = [cell.contentView viewWithTag:1];
}
startDtLbl.text = [[answers objectAtIndex:_pageView.currentPage] objectAtIndex:(indexPath.row)];
return cell; }

-(BOOL) textFieldShouldReturn:(UITextField *)textField {
UITableViewCell *textCell = [[textField superview] superview];
NSIndexPath *indexPathOfText = [_answerTable indexPathForCell:textCell];
[switchStates replaceObjectAtIndex:indexPathOfText.row withObject:textField.text];
[answers replaceObjectAtIndex:_pageView.currentPage withObject:switchStates];
[textField resignFirstResponder];
return YES; }

The bool method just assigns the text to the arrays and through logging I've found that this works perfectly. Also in case you haven't noticed, the cells contain TextFields. Any ideas on what could be wrong?

isklikas
  • 190
  • 2
  • 16
  • First of all, startDtLbl is leaking. You need to autorelease it unless you are using ARC. As for the cell content, have you stepped through cellForRowAtIndexPath and queried the array returned? – rocky Jul 19 '13 at 22:59
  • @rocky Yes I am using ARC. Yes I've queried through cellForRowAtIndexPath and I can see that the text goes to the wrong index but I can't find the reason... The moment that the array is used is too late, even though the array made is correct and the indexes are correct too (also tested). However, I've spotted that this method runs more times, even when not called. Do you think this could have caused the problem? – isklikas Jul 19 '13 at 23:15
  • What do you mean by "the text goes to the wrong index"? – rocky Jul 19 '13 at 23:31
  • 1
    So, when you get to the part where you're actually setting the text on startDtLbl, is _pageView.currentPage correct? Is that what's changing or is the indexPath.row bit acting strange? – Stakenborg Jul 19 '13 at 23:32
  • @Stakenborg it is exactly what you said, indexPath.row is acting strange, eg for the first element of the table and the array it gives 1, for the second 0, third was 2 (fine), and fourth was 3 (equally fine). However, when trying to rearrange with an if statement, now the cells were displaying in the wrong order and in the end, the bug occurred again. But I also want to try this if statement again because I believe I did a mistake in the first time I typed it. But is there anything else you would recommend for a wrong indexPath.row? – isklikas Jul 19 '13 at 23:53
  • @rocky, I mean that the something that is supposed to show on the first page shows on the 3rd page which on it's turn changes the first page. Also, the one currently being typed works, the other ones mess up after pressing return which calls the cellForRowAtIndexPath after the textFieldShouldReturn method which works perfectly (or it shows to do so) – isklikas Jul 19 '13 at 23:58

0 Answers0