0

I have a TableView with custom cell in each cell I have a text field, when I change the value of the textField, another text field in another cell is changed.

Exemple :

I changed the value in cell 0, cell 8 has also changed

tableView cellForRowAtIndexPath Method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
static NSString *CellIdentifier = @"Disbursment_CustomCell";

Disbursment_CustomCell *cell = (Disbursment_CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[Disbursment_CustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;

}

 cell.disbursmentNumber_label.text = [[NSString alloc] initWithFormat:@"%d",[indexPath row]];
return cell;
}

CustomCell.m

@implementation Disbursment_CustomCell
@synthesize dateDisbursment_textField,pourcentage_Slider,pourcentage_textField;
-(IBAction) sliderChanged:(id) sender{
      self.pourcentage_textField.text=[NSString stringWithFormat:@"%li", lroundf(self.pourcentage_Slider.value)];
}
- (void)handlePickerChanged:(id)sender
{
   UIDatePicker *picker = (UIDatePicker *)sender;
NSDate *myDate = picker.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd'/'MM'/'yyyy"];
NSString *prettyVersion = [dateFormat stringFromDate:myDate];
self.dateDisbursment_textField.text = prettyVersion;
}
- (IBAction)didBegin:(id)sender {

self.picker = [[UIDatePicker alloc] init];
self.picker.datePickerMode = UIDatePickerModeDate;
[self.picker addTarget:self action:@selector(handlePickerChanged:) forControlEvents:UIControlEventValueChanged];
dateDisbursment_textField.inputView = self.picker ;
}

@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aladin Lazhar
  • 33
  • 1
  • 5
  • 3
    can u please show me your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method?? – BhavikKama Sep 17 '13 at 11:19
  • use `textfield delegate` methods and reload particular cell . – Toseef Khilji Sep 17 '13 at 11:22
  • is cell 8 visible on screen or when you scroll down you see that it's changed? – Desdenova Sep 17 '13 at 11:38
  • Show your code in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. so one can better understand what is going wrong. – Bharat Sep 17 '13 at 11:46
  • Put a breakpoint next to all three instances of `cell.disbursmentNumber_label.text =`. One of them is getting called when you don't want it to. Either this or it's a problem with recycling of the cells - but since you are rewriting the text in every run (not just some), I don't think that will turn out to be the problem. Put the breakpoints in and watch for unintentional rewrites. – czechboy Sep 17 '13 at 14:34
  • 1
    as @Desdenova said, your cells are being reused, thats when a cell is reused you see the old values in the text field. How many textfields do you have in your table? – aahsanali Sep 17 '13 at 14:58
  • Cell 8 is not visible on screen, – Aladin Lazhar Sep 17 '13 at 17:35
  • for each cell I have 3 textfield, cell 8 is not visible in the screen, when i scrool down i see the value, I put breakpoint, it executes the method sliderChanged 3 times to change 3 text Filed or more ... – Aladin Lazhar Sep 17 '13 at 17:44

1 Answers1

0

This question might be old but for people like myself who are having the same issue, here is my solution:

I got a hint from one of the comments above saying the cell is being reused. This is the case. One can then find the prepareForReuse method as part of the UITableViewCell class. using this method (after you call super.prepareForReuse()) you can insert some lines which set the text labels back to their defaults. Any cell being reused will jump back to default then. Think of this method as a viewDidLoad type of thing, but for cells which are being reused - ones that are not currently on the screen...

NOTE: I must point out that once you scroll down and back up in your table view, those cells are being recreated using the reuse identifier. So if you make a change to one, then scroll down a little, then back up so the cell shows on the screen again, it will do whats inside the prepareForReuse method... So try keep track of a cell identity or something (I used an array which contained the id's of the cells changed).

As it stands, this i the best I have found. If I find something better I will edit my answer.

My Code for the prepareForReuse method:

override func prepareForReuse()
{
    super.prepareForReuse()
    if findInArray("\(stopName)\(routeNumber)\(lblTime.text)") == true
    {
        btnNotification.setTitle("☗", forState: UIControlState.Normal)
    }
    else
    {
        btnNotification.setTitle("☖", forState: UIControlState.Normal)
    }
}
Byron Coetsee
  • 3,533
  • 5
  • 20
  • 31