0

我在每个单元格中有一个带有自定义单元格的 TableView 我有一个文本字段,当我更改 textField 的值时,另一个单元格中的另一个文本字段被更改。

例子:

我更改了单元格 0 中的值,单元格 8 也更改了

tableView cellForRowAtIndexPath 方法

- (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
4

1 回答 1

0

这个问题可能很老,但对于像我这样有同样问题的人来说,这是我的解决方案:

我从上面的一条评论中得到了一个提示,说该单元正在被重用。情况就是这样。然后可以找到该prepareForReuse方法作为 UITableViewCell 类的一部分。使用此方法(调用后super.prepareForReuse()),您可以插入一些将文本标签设置回默认值的行。任何被重用的单元格都会跳回默认值。将此方法视为一种viewDidLoad事物,但对于正在重复使用的单元格-当前不在屏幕上的单元格...

注意:我必须指出,一旦您在表格视图中向下和向上滚动,这些单元格将使用重用标识符重新创建。因此,如果您对一个进行更改,然后向下滚动一点,然后备份,以便单元格再次显示在屏幕上,它将执行prepareForReuse方法内部的操作......所以尝试跟踪单元格身份或其他东西(我使用过一个包含单元格 id 的数组已更改)。

就目前而言,这是我发现的最好的。如果我找到更好的东西,我会编辑我的答案。

我的prepareForReuse方法代码:

override func prepareForReuse()
{
    super.prepareForReuse()
    if findInArray("\(stopName)\(routeNumber)\(lblTime.text)") == true
    {
        btnNotification.setTitle("☗", forState: UIControlState.Normal)
    }
    else
    {
        btnNotification.setTitle("☖", forState: UIControlState.Normal)
    }
}
于 2014-06-29T08:07:59.403 回答